views:

113

answers:

2

The problem: When converting any PNG image into a JPEG, the image turns all black.

To start off, I've searched the internet and stackoverflow to find out how to do this. I've tried every method I could find in the PHP Manual and on Stack Overflow. The problem still exists. I'm using GD (don't have ImageMagick installed).

My code is below. This is the call to the function:

$tempImage = $dirPath.$filename.$tempMini.".jpg";           
createTempImage($sourcefile, $tempImage, $tempMini_width, $tempMini_height, 100);

I've commented out the different methods that I've tried.

function createTempImage($sourcefile, $setNewName, $maxwidth, $maxheight, $quality){

$fileInfoArray = getimagesize($sourcefile);
$imagetype = $fileInfoArray['mime'];

if($imagetype == 'image/jpeg'){
    $img = imagecreatefromjpeg($sourcefile);

}elseif($imagetype == 'image/gif'){
    $img = imagecreatefromgif($sourcefile);

}elseif(($imagetype == 'image/png')||($imagetype == 'image/x-png')){
    $img = imagecreatefrompng($sourcefile);
}

$width = imagesx( $img );
$height = imagesy( $img );

if ($width > $maxwidth || $height > $maxheight){
    $factor = min(($maxwidth/$width),($maxheight/$height));
    $newwidth = round($width*$factor);
    $newheight = round($height*$factor);
} else {
    $newwidth = $width;
    $newheight = $height;
}   


$tmpimg = imagecreatetruecolor( $newwidth, $newheight );
imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
imagejpeg($tmpimg, $setNewName, 100);

imagedestroy($tmpimg);
imagedestroy($img);

}

The following have also been attempted:

$white = imagecolorallocate($tmpimg, 255, 255, 255);
ImageFill($tmpimg, 0, 0, $white);
ImageSaveAlpha($tmpimg, false);
ImageAlphaBlending($tmpimg, false);
$white = imagecolorallocate($tmpimg,  255, 255, 255);
imagefilledrectangle($tmpimg, 0, 0, $newwidth, $newheight, $white);

Update: The top black box is the image result: http://twitpic.com/30ywf5

+1  A: 

Just a couple of ideas:

  • $newHeight = $maxheight; seems to be a typo, "newheight" is spelled without the capital "H" throughout the code.

  • The code to determine the new size can be shortened sigificantly:

if ($width > $maxwidth || $height > $maxheight){
$factor = min(($maxwidth/$width),($maxheight/$height));
$newwidth = round($width*$factor);
$newheight = round($height*$factor); }

  • You use imagecopyresampled to create the new image - this only works in specific GD-versions ("version 2"), try to use imagecopyresized otherwise.
Select0r
@Select0r Thanks for your help. I appreciate it. Unfortunately, imagecopyresized didn't work for me.The images still are completely black. I'm picking new images each time as well, all .PNG format.
stwhite
You may want to have a look at this code: http://www.beehave.de/forum/aktuelle-version-von-uploadpic-1-3-9-t527.html It's an add-on for the phpBB-software and allows the uploading of pictures. Uploading PNGs and resizing them will result in (not black) JPGs, so maybe you can find your solution by analyzing how the upload/resizing is handled there.
Select0r
You forgot to leave in the `else`-statement after my shortened code. If you do it this way, $newwidth and $newheight may be NULL.
Select0r
Whoops. Fixed now.I've also now tried removing the function altogether and placing directly in my .php page and I still get fully black images.
stwhite
A: 

I seemed to have fixed the problem by recreating the whole function from scratch. Thank you guys for your input.

The problem was that PNG's weren't being uploaded. When executing the script with already uploaded URLs, it worked fine.

Thanks again.

stwhite