PHP Create new image using size values from database?

Hi, I’m trying to create a thumbnail image based on an image uploaded via the form and so far I have got this working all well.

My issue is that I don’t want to define the width and height of the thumbnail through the php file, instead this should resize based on a height and width field values in the database.

above the code where it creates the image I have made a database query and assigned these two values to :
$the_thumb_width and $the_thumb_height

When I replace the static values in the file with the variables, I get an error and the file is not created. The error is:

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /web/…

I echo’d out the variables to test if they are being pulled in and they are, so I don’t understand why this is occurring. Can anyone help? I’m pasting the area of the code that generates the thumbnail. Thanks! :slight_smile:

[php]

//-------------------- create thumbnail --------------------------------------------

function createthumb($src_filename, $dst_filename_thumb)
{
$size = getimagesize($src_filename);
$stype = $size[‘mime’];
$w = $size[0];
$h = $size[1];
switch($stype) {
case ‘image/gif’:
$simg = imagecreatefromgif($src_filename);
//header(“Content-type: image/gif”);
break;
case ‘image/jpeg’:
$simg = imagecreatefromjpeg($src_filename);
//header(“Content-type: image/jpg”);
break;
case ‘image/png’:
$simg = imagecreatefrompng($src_filename);
//header(“Content-type: image/png”);
break;
}

// get image width
$width = $w;
// get image height
$height = $h;

// size to create thumnail width
$thumb_width = $the_thumb_width;
$thumb_height = $the_thumb_width;


$dimg = imagecreatetruecolor($thumb_width, $thumb_height);
$wm = $width/$thumb_width;
$hm = $height/$thumb_height;
$h_height = $thumb_height/2;
$w_height = $thumb_width/2;
if($w> $h) {
	$adjusted_width = $w / $hm;
	$half_width = $adjusted_width / 2;
	$int_width = $half_width - $w_height;
	imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$thumb_height,$w,$h);
} elseif(($w <$h) || ($w == $h)) {
	$adjusted_height = $h / $wm;
	$half_height = $adjusted_height / 2;
	$int_height = $half_height - $h_height;
	imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$thumb_width,$adjusted_height,$w,$h);
} else {
	imagecopyresampled($dimg,$simg,0,0,0,0,$thumb_width,$thumb_height,$w,$h);
}
	
$ran = "thumb_".rand () ;
global $thumb2;	
$thumb2 = $ran.".jpg";

global $thumb_Add_thumb;
$thumb_Add_thumb = $dst_filename_thumb;
$thumb_Add_thumb .= $thumb2;
	

 imagejpeg($dimg, "" .$dst_filename_thumb. "$thumb2"); 

[/php]

[php]// size to create thumnail width
$thumb_width = $the_thumb_width;
$thumb_height = $the_thumb_width;
[/php]

Should one of these be $the_thumb_height ? Good luck. Hope this helps.

sorry, yes I had fixed the one referencing the height to $the_thumb_height. Still have the same issue though. Does anyone know what the issue could be?

Thanks

as the resize is contained in a function you need to pass the with and heigh to the function too otherwise there in the wrong scope.

[php]function createthumb($src_filename, $dst_filename_thumb, $the_thumb_width, $the_thumb_height)[/php]

Can I ask where you got the code originally? only just wondering if you came across it on my tutorial site as its nearly identical http://www.phphelptutorials.com/tutorials/php-tutorials/resize-images-on-upload/

Sponsor our Newsletter | Privacy Policy | Terms of Service