Webstie Designer Webstie DesignerWebstie DesignerWebstie DesignerWebstie Designer
 
 
Cheap Webstie Designer Cheap Webstie Designer Cheap Webstie Designer Cheap Webstie Designer Cheap Webstie Designer Cheap Webstie Designer
 
 

How To Create Thumb nails in PHP

When you resize a large image it takes ages to load and if you have a page of these images such as search results or a gallery it can be a nightmare for users so the answer is to create thumbnails. Below is a modified script I use to create thumbnails

function createthumbnail($name,$filename,$new_w,$new_h,$oldx,$oldy)
{
$system=explode(".",$name);
if ($system[1] == "jpg"){$src_img=imagecreatefromjpeg($name);}
if ($system[1] == "JPG"){$src_img=imagecreatefromjpeg($name);}
if ($system[1] == "jpeg"){$src_img=imagecreatefromjpeg($name);}
if ($system[1] == "JPEG"){$src_img=imagecreatefromjpeg($name);}
if ($system[1] == "png"){$src_img=imagecreatefrompng($name);}
if ($system[1] == "PNG"){$src_img=imagecreatefrompng($name);}

$old_x=$oldx;
$old_y=$oldy;
$thumb_w=$new_w;
$thumb_h=$new_h;

$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresized($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if ($system[1] == "PNG" || $system[1] == "png")
{
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}

Now this function will work fine, however due to the processing power required to perform these calculations you will need to up your PHP.ini stats

upload_max_filesize = 20M;
memory_limit = 512m;
max_execution_time = 460;
log_errors_max_len = 4048;
max_input_time = 360;
post_max_size = 64m;

This is very extreme however it should deal with most images then. There is a problem I have found on images with more than 3000*3000 pixels. It wont accept them and will return an error. Now this is not due to the process time or memory limited needed for this is only just over 8mb ((3000*3000)/(1024*1024)) however with a limit of 512m it should handle it but does not. I believed this was a server problem but after trying on a few servers it will not work.

There for I believe it is a restriction on the imagecreatefromjpeg command that cannot handle images of that larger dimension. The solution for now is to put a get out clause in your code and if the dimensions are to big then just rename the image with your thumbnail extension such as

function createthumb2($name,$filename,$new_w,$new_h,$oldx,$oldy)
{
$file = $name;
$newfile = $filename;

if (!copy($file, $newfile))
{
echo "failed to copy $file...\n";
}
}


This will mean that the really large images will be copied with the correct name but will need to be resized.

The alternative is to not allow images of those dimensions to be entered into the system but then the user has to work out how to resize them manually. Also note is does not matter how big the image is in terms of file size it’s the dimensions that count.

Hope this helps someone any problems with this Email Me

 
  Webstie DesignerCheap Webstie Designer