Page 1 of 2
A blank, black image!!!
Posted: Tue Dec 04, 2007 9:02 pm
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()?
Posted: Tue Dec 04, 2007 9:12 pm
by feyd
I'm pretty sure we've talked about this before…
Posted: Tue Dec 04, 2007 9:13 pm
by volka
imagecopyresize(... $maxW, maxH, $uploadedImgWidth, ...)
Is this script running with error_reporting=E_ALL?
Posted: Tue Dec 04, 2007 9:51 pm
by JellyFish
volka wrote:imagecopyresize(... $maxW, maxH, $uploadedImgWidth, ...)
Is this script running with error_reporting=E_ALL?
I don't know what that means?
Posted: Tue Dec 04, 2007 9:53 pm
by feyd
Lack of a $ on maxH…
Posted: Tue Dec 04, 2007 10:05 pm
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 
Posted: Tue Dec 04, 2007 10:35 pm
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?
Posted: Tue Dec 04, 2007 10:52 pm
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.
Posted: Tue Dec 04, 2007 11:12 pm
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?
Posted: Wed Dec 05, 2007 1:34 am
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.
Posted: Wed Dec 05, 2007 3:19 am
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!

Posted: Wed Dec 05, 2007 3:20 am
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.
Posted: Wed Dec 05, 2007 3:57 am
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?
Posted: Wed Dec 05, 2007 4:24 am
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.
Posted: Wed Dec 05, 2007 5:33 am
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()?