Valhalla Legends Forums Archive | Web Development | Generating Thumbnails

AuthorMessageTime
Barabajagal
Last night, I wrote this code to generate a thumbnail no greater than 250x250 pixels from any JPG, GIF, or PNG file online. Today, I modified it so it won't write to the server at all. I'd like to know if it can be improved in any way?
[code]<?php
/* PHP Thumbnail Generator
    Written by
    RealityRipple Software
    September 24, 2008 */
$maxSize = 250;
$quality = 80;
$imgURL = str_replace(' ','%20',$_REQUEST['url']);
$ext = substr($imgURL, -3);
if (strtolower($ext) == 'jpg')
{
  $img = imagecreatefromjpeg($imgURL);
}
else if (strtolower($ext) == 'gif')
{
  $img = imagecreatefromgif($imgURL);
}
else if (strtolower($ext) == 'png')
{
  $img = imagecreatefrompng($imgURL);
}
else
{
  die();
}
$pWid = imagesx($img);
$pHgh = imagesy($img);
if ($pWid > $maxSize || $pHgh > $maxSize)
{
  if ($pWid < $pHgh)
  {
  $tHgh = $maxSize;
  $tWid = $pWid / ($pHgh / $maxSize);
  }
  else if ($pHgh < $pWid)
  {
  $tHgh = $pHgh / ($pWid / $maxSize);
  $tWid = $maxSize;
  }
  else
  {
  $tHgh = $maxSize;
  $tWid = $maxSize;
  }
}
else
{
  $tHgh = $pHgh;
  $tWid = $pWid;
}
$thumb = imagecreatetruecolor($tWid, $tHgh);
imagecopyresampled($thumb, $img, 0, 0, 0, 0, $tWid, $tHgh, $pWid, $pHgh);
ob_start();
  imagejpeg($thumb, NULL, $quality);
  $tData = ob_get_contents();
  $tLen = ob_get_length();
ob_end_clean();
Header("Content-Type: image/jpeg");
Header("Content-Length: $tLen");
echo $tData;
imagedestroy($img);
imagedestroy($thumb);
?>[/code]

Oh, and can someone help me get it to work with this image (Warning: huge file)? I already tried setting the memory_limit to 100M and even -1.
September 24, 2008, 7:45 PM
iago
The code is extremely hard to read, and a bunch of "#160"s seem to be getting stuck in there. Anybody know what the deal is?
September 24, 2008, 9:03 PM
Barabajagal
Eww, what the hell happened? Edit: repasted and they seem to have gone away. I still don't like the coloring, though.
September 24, 2008, 9:07 PM
K
You could use urldecode() instead of replacing %20 with space manually.  That way it would also work for other encoded characters.
September 25, 2008, 3:38 AM
K
[quote author=K link=topic=17670.msg179948#msg179948 date=1222313935]
You could use urlencode()/urldecode() instead of swapping %20 with space manually.  That way it would also work for other encoded characters.
[/quote]
September 25, 2008, 3:39 AM
Barabajagal
Tried it, it didn't work because it converts too much stuff (/ for example).
September 25, 2008, 4:35 AM

Search