A blank, black image!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

A blank, black image!!!

Post by JellyFish »

Code: Select all

function changeImagen($name, $image)
{
	list($uploadedImgWidth, $uploadedImgHeight, $uploadedImgType) = getimagesize($image['tmp_name']);
	
	if (substr(image_type_to_mime_type($uploadedImgType), 0, 5) != "image")
	{
		return "<script type='text/javascript'>alert("Unable to upload your image. File doesn't appear to be an image.");</script>";
	}
	
	$maxW = 150;
	$maxH = 100;
	
	$dimensionRatio = $uploadedImgWidth/$uploadedImgHeight;
	
	if ($maxW/$maxH > $dimensionRatio)
	{
	   $maxW = $maxH*$dimensionRatio;
	}
	else
	{
	   $maxH = $maxW/$dimensionRatio;
	}
	
	$resizedImg = imagecreatetruecolor($maxW, $maxH);
	$uploadedImg = imagecreatefromjpeg($eyecon['tmp_name']);
	
	imagecopyresize($resizedImg, $uploadedImg, 0, 0, 0, 0, $maxW, maxH, $uploadedImgWidth, $uploadedImgHeight) or die("<script>alert('Failed at resizing!')</script>");
	
	imagejpeg($resizedImg, "".$_SESSION['username']."/".$name.".jpg", 75) or die("<script>alert('Fialed at saving file!');</script>");
	
	$connection = mysql_connect("IP", "username", "password") or die(msql_error());
	$db = mysql_select_db("db", $connection) or die(mysql_error());
	
	$results = mysql_query("UPDATE `table` SET `picture` = '$_SESSION[username]/$name.jpg' WHERE `username` = '$_SESSION[username]' AND `name` = '$name'") or die(mysql_error());
	
	return "<script type='text/javascript'>alert('done');</script>";
}
The result is a resized black, blank image! Why? What did I do wrong? I've been racking my brain over this and I hope that I've been looking to hard and that it's more simple then I think.

*sigh* Oh boy...
PHP.Net wrote:Note: There is a problem due to palette image limitations (255+1 colors). Resampling or filtering an image commonly needs more colors than 255, a kind of approximation is used to calculate the new resampled pixel and its color. With a palette image we try to allocate a new color, if that failed, we choose the closest (in theory) computed color. This is not always the closest visual color. That may produce a weird result, like blank (or visually blank) images. To skip this problem, please use a truecolor image as a destination image, such as one created by imagecreatetruecolor().
I'm using imagecreatetryecolor()?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm pretty sure we've talked about this before…
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

imagecopyresize(... $maxW, maxH, $uploadedImgWidth, ...)
Is this script running with error_reporting=E_ALL?
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

volka wrote:
imagecopyresize(... $maxW, maxH, $uploadedImgWidth, ...)
Is this script running with error_reporting=E_ALL?
I don't know what that means?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Lack of a $ on maxH…
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

JellyFish wrote:
volka wrote:
imagecopyresize(... $maxW, maxH, $uploadedImgWidth, ...)
Is this script running with error_reporting=E_ALL?
I don't know what that means?
http://php.net/error_reporting ;)
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

It was the $ I think. Thanks for pointing that out feyd.

It works now.

I don't know how to check the error reporting.

The only thing wrong with my script now is that it only works with jpgs.

Is there more cross-format functions that I could use in place of:
  • imagecreatefromjpeg()
  • imagejpeg()
So that I don't return only jpeg image resources. Or would I have to check the mime type of every the image and for every image mime type(image/jpeg, image/png, image/gif) call the appropriate functions?

Also if I saved the image to a location with the extension .png when it's mime type is image/jpg would I come into trouble?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I don't know how to check the error reporting.
Did you read the link I sent you?
So that I don't return only jpeg image resources. Or would I have to check the mime type of every the image and for every image mime type(image/jpeg, image/png, image/gif) call the appropriate functions?
Yes you need check the image type before applying the correct function.
Also if I saved the image to a location with the extension .png when it's mime type is image/jpg would I come into trouble?
Changing the file extension of the file doesn't change it's contents. So no.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Jcart wrote:Changing the file extension of the file doesn't change it's contents. So no.
Would it hinder how the browser reads the file? Should I save the image with the appropriate extension as well?

Now, is my method of detecting the uploaded image mime-type guaranteed, or is it faulty?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Have a look at imagejpeg(), imagegif(), and imagepng(). Send the appropriate header for their content-type. And yes, if you're determining what kind of image it is by it's extension, it is very faulty. Use index 2 of getimagesize() to determine what kind of image it is.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

scottayy wrote:Have a look at imagejpeg(), imagegif(), and imagepng(). Send the appropriate header for their content-type. And yes, if you're determining what kind of image it is by it's extension, it is very faulty. Use index 2 of getimagesize() to determine what kind of image it is.
All very good information.

Now I'll be off to code this out.

Thank you all very much for this! You guys are the best! :D
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

If you trying to resize a palette image first you have to make a true color image with the dimensions, copy (imagecopymerge) the original on to the new one,
resize then transform the resized image into palette image using imagetruecolortopalette. This of course after you read the manual on how to use this functions.
EDIT: The function you are trying to resize it' s a transparent gif or png because then you have to save the transparent pixels using imagesavealpha, imagecolortransparent and imagealphablending. This is a bit more complicated.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Rovas wrote:If you trying to resize a palette image first you have to make a true color image with the dimensions, copy (imagecopymerge) the original on to the new one,
resize then transform the resized image into palette image using imagetruecolortopalette. This of course after you read the manual on how to use this functions.
EDIT: The function you are trying to resize it' s a transparent gif or png because then you have to save the transparent pixels using imagesavealpha, imagecolortransparent and imagealphablending. This is a bit more complicated.
I'm not quite sure I understand.

But I am having another problem with my script. How do I set the background color or transparency of $resizedImg? It's black right now even with transparent gifs and pngs. How can I set the background color to transparent with gifs and pngs, and white with jpgs?
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Sorry I forgot about this link There you will find a script that does everything you need when it comes to resizing images. Or use it as example the part for saving image with transparent pixels.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Alrighty thanks for this. This is very good.

But I'm having a problem:

Code: Select all

$uploadedImg = (imagecreatefromjpeg($eyecon['tmp_name']) or imagecreatefrompng($eyecon['tmp_name'])) or imagecreatefromgif($eyecon['tmp_name']) or die("Unable to create image!");
Will this work, because it's not appearing to? Or am I able to use $_FILES['tmp_name'] as a second parameter to imagecopyresampled()?
Post Reply