• Welcome to Valhalla Legends Archive.
 

Generating Thumbnails

Started by Barabajagal, September 24, 2008, 02:45 PM

Previous topic - Next topic

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?
<?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);
?>


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.

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?
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Barabajagal

Eww, what the hell happened? Edit: repasted and they seem to have gone away. I still don't like the coloring, though.

K

You could use urldecode() instead of replacing %20 with space manually.  That way it would also work for other encoded characters.

K

Quote from: K on September 24, 2008, 10:38 PM
You could use urlencode()/urldecode() instead of swapping %20 with space manually.  That way it would also work for other encoded characters.

Barabajagal

Tried it, it didn't work because it converts too much stuff (/ for example).