Jump to content

image resize problem


hakimserwa

Recommended Posts

i am tryin to resize my image upoad for a thumbnail but i keep geting this error massege.

 

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

 

icant seem to figure this out help mates

this i the code

 

$sql2 = mysql_query("INSERT INTO files (id, userid, fname, lname, email)
	VALUES
	('', '$lastInsertedId', '$first_name', '$last_name', '$email_address')") or die('Error: ' . mysql_error());

	$lastInsertedId = mysql_insert_id(); //lets make a thumb for all clients.

	$thumbDir = "uploads/thumbs/";
	$thumbWidth = 200;
	$thumbHeight = 150;
	$fileExt = strtolower($fileExt);
	$originalImage = "uploads/$fileName";
	$newThumb = $thumbDir . $lastInsertedId ."_". $fileName;

	$scale_ratio = $width / $height;
	if (($thumbWidth / $thumbHeight) > $scale_ratio) {
		   $thumbWidth = $thumbHeight * $scale_ratio;
	} else {
		   $thumbHeight = $thumbWidth / $scale_ratio;
	}

	if($type==2){
		$oldImage = imagecreatefromjpeg($originalImage);	
	}elseif($type==1){
		$oldImage = imagecreatefromgif($originalImage);	
	}elseif($type==3){
		$oldImage = imagecreatefrompng($originalImage);	
	}

echo $width. " "; echo $height ." "; echo $thumbWidth. " "; echo$thumbHeight;
	$thumb = imagecreatetruecolor($thumbWidth, $thumbheight);
	imagecopyresampled($thumb, $oldImage, 0,0,0,0, $thumbWidth, $thumbHeight, $width, $height);
	imagejpeg($thumb, $newThumb);

Link to comment
Share on other sites

I think you need to get the width and height of the old image. At the moment you are not defining the $width and $height variables.

 

Above this line.

 

imagecopyresampled($thumb, $oldImage, 0,0,0,0, $thumbWidth, $thumbHeight, $width, $height);

 

add this.

 

list($width,$height) = getimagesize($originalImage);

Link to comment
Share on other sites

Try removing the following lines of code:

 

$scale_ratio = $width / $height;
if (($thumbWidth / $thumbHeight) > $scale_ratio) {
   $thumbWidth = $thumbHeight * $scale_ratio;
} else {
   $thumbHeight = $thumbWidth / $scale_ratio;
}

 

and set the variables $thumbHeight and $thumbWidth with set values just for testing purposes. Does that work?

Link to comment
Share on other sites

Remove .

 

$scale_ratio = $width / $height;
if (($thumbWidth / $thumbHeight) > $scale_ratio) {
   $thumbWidth = $thumbHeight * $scale_ratio;
} else {
   $thumbHeight = $thumbWidth / $scale_ratio;
}

 

and replace with

 

$thumbWidth = 100;
$thumbHeight = 50;

 

Does that work?

Link to comment
Share on other sites

ihave seen the problem. it was lying in the new image height valuable, spelling error

 

before it was $thumb = imagecreatetruecolor($thumbWidth, $thumbheight);

 

and changed the h in the valuable it a capital H

 

$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);

Link to comment
Share on other sites

Well there a few things bits that stood out.

 

You have the following line of code:

$fileExt = strtolower($fileExt);

 

But you are not exactly checking the extension anywhere from what I have seen. You are also using a variable called type to select which type of image gets created but I can't see that defined anywhere in your code either. Why not check the file extension and then use that instead of type?

 

 

Link to comment
Share on other sites

 

Off topic, but you can convert the following from this:

if($type==2){
$oldImage = imagecreatefromjpeg($originalImage);	
}elseif($type==1){
$oldImage = imagecreatefromgif($originalImage);	
}elseif($type==3){
$oldImage = imagecreatefrompng($originalImage);	
}

 

to this:

$originalImage = "uploads/$fileName";
$string = file_get_contents($originalImage);
$oldImage = imagecreatefromstring($string);

 

 

<?php
$sql2 = mysql_query("INSERT INTO files (id, userid, fname, lname, email)
	VALUES
	('', '$lastInsertedId', '$first_name', '$last_name', '$email_address')") or die('Error: ' . mysql_error());

$lastInsertedId = mysql_insert_id(); //lets make a thumb for all clients.

$thumbDir = "uploads/thumbs/";
$thumbWidth = 200;
$fileExt = strtolower($fileExt);
$originalImage = "uploads/$fileName";
$details = getimagesize($originalImage) or die("invalid image format.");
$newThumb = $thumbDir . $lastInsertedId ."_". $fileName;

$scale_ratio = $width / $height;
$thumbHeight = $details[1] * ($thumbWidth / $details[0]); 

$string = file_get_contents($originalImage);
$oldImage = imagecreatefromstring($string);

echo $width. " "; echo $height ." "; echo $thumbWidth. " "; echo$thumbHeight;
$thumb = imagecreatetruecolor($thumbWidth, $thumbheight);
imagecopyresampled($thumb, $oldImage, 0,0,0,0, $thumbWidth, $thumbHeight, $width, $height);
imagejpeg($thumb, $newThumb);

Link to comment
Share on other sites

Well there a few things bits that stood out.

 

You have the following line of code:

$fileExt = strtolower($fileExt);

 

But you are not exactly checking the extension anywhere from what I have seen. You are also using a variable called type to select which type of image gets created but I can't see that defined anywhere in your code either. Why not check the file extension and then use that instead of type?

 

i had it taken care of from the top even though it wasnt in the script that i posted.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.