egoselfaxis
asked on
Need help fixing (or finding) PHP-based thumbnail image generating script
I'm having a really hard time getting the attached PHP-based thumbnail image generating script to work on my windows-based shared hosting account.
The errors that I'm getting when I try to apply the createthumb() function -- using 100% valid image resources --- are as follows:
Notice: Undefined offset: 1 in D:\Inetpub\antiqueplace\we bauction\u pload\thum bnail_gene rator.php on line 92
Notice: Undefined variable: tmparr
Notice: Undefined variable: src_img
Warning: imagesx(): supplied argument is not a valid Image resource
Warning: imagesy(): supplied argument is not a valid Image resource
Notice: Undefined variable: src_img
Warning: imagecopyresampled(): supplied argument is not a valid Image resource
Warning: imagedestroy(): supplied argument is not a valid Image resource
(etc..)
It's confusing because I've gotten this script to work without any problems several times already (on the same shared hosting environment) .. yet for some reason I can't get the script to work properly in this case. Why is this script failing? How can I fix it?
If anyone knows of a better way for me to create thumbnail images using PHP ... I'd be open to replacing what I've got with it.
Thanks,
- Yvan
The errors that I'm getting when I try to apply the createthumb() function -- using 100% valid image resources --- are as follows:
Notice: Undefined offset: 1 in D:\Inetpub\antiqueplace\we
Notice: Undefined variable: tmparr
Notice: Undefined variable: src_img
Warning: imagesx(): supplied argument is not a valid Image resource
Warning: imagesy(): supplied argument is not a valid Image resource
Notice: Undefined variable: src_img
Warning: imagecopyresampled(): supplied argument is not a valid Image resource
Warning: imagedestroy(): supplied argument is not a valid Image resource
(etc..)
It's confusing because I've gotten this script to work without any problems several times already (on the same shared hosting environment) .. yet for some reason I can't get the script to work properly in this case. Why is this script failing? How can I fix it?
If anyone knows of a better way for me to create thumbnail images using PHP ... I'd be open to replacing what I've got with it.
Thanks,
- Yvan
<?php
$imagefolder='.';
$thumbsfolder='.';
$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"tn_");
if ($pics[0]!="")
{
foreach ($pics as $p)
{
createthumb($p,"tn_".$p,150,150);
}
}
/*
Function ditchtn($arr,$thumbname)
filters out thumbnails
*/
function ditchtn($arr,$thumbname)
{
foreach ($arr as $item)
{
if (!preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;}
}
return $tmparr;
}
/*
Function createthumb($name,$filename,$new_w,$new_h)
creates a resized image
variables:
$name Original filename
$filename Filename of the resized image
$new_w width of resized image
$new_h height of resized image
*/
function createthumb($name,$filename,$new_w,$new_h)
{
$system=explode(".",$name);
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y)
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y)
{
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y)
{
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}
/*
Function directory($directory,$filters)
reads the content of $directory, takes the files that apply to $filter
and returns an array of the filenames.
You can specify which files to read, for example
$files = directory(".","jpg,gif");
gets all jpg and gif files in this directory.
$files = directory(".","all");
gets all files.
*/
function directory($dir,$filters)
{
$handle=opendir($dir);
$files=array();
if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}
if ($filters != "all")
{
$filters=explode(",",$filters);
while (($file = readdir($handle))!==false)
{
for ($f=0;$f<sizeof($filters);$f++):
$system=explode(".",$file);
if ($system[1] == $filters[$f]){$files[] = $file;}
endfor;
}
}
closedir($handle);
return $files;
}
?>
ASKER
Is there any possibility of getting something a bit simpler -- perhaps something that handles the thumbnail creation for a single JPEG image?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The calling sequence for that function is on lines 38 and 39.
HTH, ~Ray
HTH, ~Ray
$imageblob = create_right_size_image($jpg);
imagejpeg($imageblob, $thumb);
ASKER
Thanks Ray!
- Yvan
- Yvan
Thanks for the points, Yvan - it's a great question! ~Ray
Best regards, ~Ray
Open in new window