...

View Full Version : Resolved Creating thumbnails



friz
12-06-2010, 08:54 PM
Hi,

I have a picture upload script. I'd like to add automatic thumbnail creation.

I tried to adapt this code (http://icant.co.uk/articles/phpthumbnails/), but I'm getting some errors.

This is my upload script


$i = $teller['aantal'];
$NewPosition = $teller['MaxPosition'];

while(list($key,$value) = each($_FILES[images][name]))
{
$i++;
$NewPosition++;
if(!empty($value))
{
list($width) = getimagesize($_FILES['images']['tmp_name'][$key]);
$filename = $value;

if($width > 900)
{
echo "<div class='fout'>
<img src='../images/error-icon.png' alt='Error' width='16' height='16'/>Error message</div>
<p>&nbsp;</p>";
/*== uploaded file verwijderen ==*/
unlink($filename);
}
else
{
$filename = $muz_foto."-".$i.".jpg";
$add = "../images/band/$muz_foto/$filename";
echo "<div class='succesvol'>Succes message</div>
<p>&nbsp;</p>";

copy($_FILES[images][tmp_name][$key], $add);
//thumbnail
createthumb('../images/band/'.$muz_foto.'/'.$filename,'../images/band/'.$muz_foto.'/thumbs/'.$filename,200,200);



}
}
}


This is the function:


function createthumb($orig_img,$thumbnail,$new_w,$new_h)
{
echo"bestandsnaam = ".$orig_img." ";
$system=explode(".",$orig_img);
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($orig_img);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($orig_img);}
print $src_img;
$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,$thumbnail);
} else {
imagejpeg($dst_img,$thumbnail);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}


I'm getting these errors:


bestandsnaam = ../images/band/xandee/xandee-6.jpg
Warning: imagesx() expects parameter 1 to be resource, null given in G:\xampp\htdocs\drfred2\includes\functions.php on line 120

Warning: imagesy() expects parameter 1 to be resource, null given in G:\xampp\htdocs\drfred2\includes\functions.php on line 121

Warning: imagecopyresampled() expects parameter 2 to be resource, null given in G:\xampp\htdocs\drfred2\includes\functions.php on line 138

Warning: imagedestroy() expects parameter 1 to be resource, null given in G:\xampp\htdocs\drfred2\includes\functions.php on line 146


So it looks like there's a problem with $src_img, but I can't figure out what it is exactly.
Maybe the parameters/paths here should be different??

createthumb('../images/band/'.$muz_foto.'/'.$filename,'../images/band/'.$muz_foto.'/thumbs/'.$filename,200,200);

GD info

GD Support enabled
GD Version bundled (2.0.34 compatible)
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.3.11
T1Lib Support enabled
GIF Read Support enabled
GIF Create Support enabled
JPEG Support enabled
libJPEG Version 7
PNG Support enabled
libPNG Version 1.2.40
WBMP Support enabled
XBM Support enabled
JIS-mapped Japanese Font Support enabled

Directive Local Value Master Value
gd.jpeg_ignore_warning 0 0

Thank you in advance ;)

Fumigator
12-06-2010, 09:20 PM
Looks like $src_img isn't an image resource. Probably because neither of your "if" conditions evaluate to TRUE, so $src_img doesn't get set to anything. You should probably put the call to imageSX() within the "if" statements, or within another "if" statement of its own, to handle this situation.

friz
12-06-2010, 09:35 PM
Thx for the (very quick) reply.

It took it out of the "if" and that worked :D


if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($orig_img);}
to

$src_img=imagecreatefromjpeg($orig_img);

Although I don't really understand why. :confused:

Fumigator
12-06-2010, 09:41 PM
copy/paste FTW I guess :rolleyes:

MattF
12-06-2010, 09:46 PM
There's another thread here from earlier along the same lines:

http://codingforums.com/showthread.php?t=210946

The function may be useful to you.

friz
12-06-2010, 10:05 PM
copy/paste FTW I guess :rolleyes:

Me changing the code was probably the problem. :D
Anyway, I'll put een file type check in my upload script. I don't need to copy/paste for that ;)


There's another thread here from earlier along the same lines:

http://codingforums.com/showthread.php?t=210946

The function may be useful to you.
That script from blazonry was my first google result, but this one above looked better for what I needed/know (I thought)

I will certainly have a look at the script you provided. Looks like I can learn from that.
Thx!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum