PHP PNG Photo upload that keeps transparency - php

I'm completely stumpted on this one... I need a photo upload script that keeps the transparency of png files. At the moment the transparent areas are black. Any help is greatly appreciated. Here is my code:
if(isset($_POST)) {
require_once('siteInfo.php');
session_start();
$ThumbSquareSize = 90;
$BigImageMaxSize = $_POST['size'];
$ThumbPrefix = "thumb_";
$DestinationDirectory = '../Uploads/';
$Quality = 100;
if (!isset($_FILES['ImageFile']) || !is_uploaded_file($_FILES['ImageFile']['tmp_name'])) {
die('Please select a file.');
}
$RandomNumber = rand(0, 9999999999);
$ImageName = str_replace(' ','-',strtolower($_FILES['ImageFile']['name']));
$ImageSize = $_FILES['ImageFile']['size'];
$TempSrc = $_FILES['ImageFile']['tmp_name'];
$ImageType = $_FILES['ImageFile']['type'];
switch (strtolower($ImageType)) {
case 'image/png':
$CreatedImage = imagecreatefrompng($_FILES['ImageFile']['tmp_name']);
break;
case 'image/gif':
$CreatedImage = imagecreatefromgif($_FILES['ImageFile']['tmp_name']);
break;
case 'image/jpeg':
case 'image/pjpeg':
$CreatedImage = imagecreatefromjpeg($_FILES['ImageFile']['tmp_name']);
break;
default:
// die('Unsupported file format. Please upload a PNG, GIF, or JPG file.'); //output error and exit
}
list($CurWidth,$CurHeight)=getimagesize($TempSrc);
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$ImageName = preg_replace("/\\.[^.\\s]{3,4}$/", "", $ImageName);
$NewImageName = $ImageName.'-'.$RandomNumber.'.'.$ImageExt;
$thumb_DestRandImageName = $DestinationDirectory.$ThumbPrefix.$NewImageName; //Thumb name
$DestRandImageName = $DestinationDirectory.$NewImageName; //Name for Big Image
if (resizeImage($CurWidth,$CurHeight,$BigImageMaxSize,$DestRandImageName,$CreatedImage,$Quality,$ImageType)) {
if(!cropImage($CurWidth,$CurHeight,$ThumbSquareSize,$thumb_DestRandImageName,$CreatedImage,$Quality,$ImageType)) {
echo 'Error Creating thumbnail';
}
$query = "Update Content set ".$_POST['field']."='".$NewImageName."' WHERE ID=".$_POST['ID'];
mysqli_query($dbc, $query);
mysqli_close($dbc);
echo 'Uploaded file: <span>'.$NewImageName.'</span>.';
} else {
die('Resize Error');
}
}
function resizeImage($CurWidth,$CurHeight,$MaxSize,$DestFolder,$SrcImage,$Quality,$ImageType) {
if ($CurWidth <= 0 || $CurHeight <= 0) {
return false;
}
$ImageScale = min($MaxSize/$CurWidth, $MaxSize/$CurHeight);
$NewWidth = ceil($ImageScale*$CurWidth);
$NewHeight = ceil($ImageScale*$CurHeight);
$NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
if (imagecopyresampled($NewCanves, $SrcImage,0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight)) {
switch(strtolower($ImageType)) {
case 'image/png':
imagepng($NewCanves,$DestFolder);
break;
case 'image/gif':
imagegif($NewCanves,$DestFolder);
break;
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves,$DestFolder,$Quality);
break;
default:
return false;
}
if (is_resource($NewCanves)) { imagedestroy($NewCanves); }
return true;
}
}
function cropImage($CurWidth,$CurHeight,$iSize,$DestFolder,$SrcImage,$Quality,$ImageType){
if ($CurWidth <= 0 || $CurHeight <= 0) {
return false;
}
if ($CurWidth>$CurHeight) {
$y_offset = 0;
$x_offset = ($CurWidth - $CurHeight) / 2;
$square_size = $CurWidth - ($x_offset * 2);
} else {
$x_offset = 0;
$y_offset = ($CurHeight - $CurWidth) / 2;
$square_size = $CurHeight - ($y_offset * 2);
}
$NewCanves = imagecreatetruecolor($iSize, $iSize);
if (imagecopyresampled($NewCanves, $SrcImage,0, 0, $x_offset, $y_offset, $iSize, $iSize, $square_size, $square_size)) {
switch(strtolower($ImageType)) {
case 'image/png':
imagepng($NewCanves,$DestFolder);
break;
case 'image/gif':
imagegif($NewCanves,$DestFolder);
break;
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves,$DestFolder,$Quality);
break;
default:
return false;
}
if (is_resource($NewCanves)) { imagedestroy($NewCanves); }
return true;
}
}

I added the following to the "resizeImage" function:
imagealphablending($NewCanves, false);
imagesavealpha($NewCanves, true);
imagealphablending($SrcImage, true);
And that fixed the problem.

Related

Image resize bug

I have inherited a function which resizes images. It works well in most cases, but for some reason, in some cases the result of resizing the image is totally different than the image initially contained. The function is as follows:
function image_resize($source, $destination, $width, $height, $resizeMode='fit', $type = 'jpeg', $options = array()) {
$defaults = array(
'output' => 'file',
'isFile' => true,
'quality' => '100',
'preserveAnimation' => false,
'offsetTop' => 0,
'offsetLeft' => 0,
'offsetType' => 'percent'
);
foreach ($defaults as $k => $v) {
if (!isset($options[$k])) {
$options[$k] = $v;
}
}
if ($options['isFile']) {
$image_info = getimagesize($source);
$image = null;
switch ($image_info[2]) {
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($source);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($source);
break;
case IMAGETYPE_GIF:
$image = imagecreatefromgif($source);
break;
case IMAGETYPE_BMP:
$image = imagecreatefromwbmp($source);
break;
default :
return false;
}
} else {
$image = imagecreatefromstring($source);
}
//we have an image resource
$iwidth = imagesx($image);
$iheight = imagesy($image);
//We need $width and $height for this call
if (QM::isAcceptableProfilePhotoSize($width, $height) == false)
{
throw new Exception("Size of ".$width."x".$height." is not supported");
}
//determine ratios
$wratio = $width / $iwidth;
$hratio = $height / $iheight;
$mratio = min(array($wratio, $hratio));
$rimage = null;
switch ($resizeMode) {
case 'fit':
$rimage = imagecreatetruecolor($iwidth * $mratio, $iheight * $mratio);
$image = imagecopyresampled($rimage, $image, 0, 0, 0, 0, $iwidth * $mratio, $iheight * $mratio, $iwidth, $iheight);
break;
case 'crop':
$rratio = $width / $height;
if ($rratio < 1) {
$nwidth = $iwidth;
$nheight = $iwidth * 1/$rratio;
if ($nheight>$iheight) {
$nwidth = $nwidth*$iheight/$nheight;
$nheight = $iheight;
}
} else {
$nwidth = $iheight*$rratio;
$nheight = $iheight;
if ($nwidth>$iwidth) {
$nheight = $nheight*$iwidth/$nwidth;
$nwidth = $iwidth;
}
}
switch ($options['offsetType']) {
case 'percent':
$sx = ($iwidth-$nwidth)*$options['offsetLeft']/100;
$sy = ($iheight-$nheight)*$options['offsetTop']/100;
break;
default :
return false;
}
$rimage = imagecreatetruecolor($width, $height);
$image = imagecopyresampled($rimage, $image, 0, 0, $sx, $sy, $width, $height, $nwidth, $nheight);
break;
default :
return false;
break;
}
if (!is_writeable(dirname($destination))) {
throw new Exception(getcwd(). "/" .dirname($destination)." is not writeable");
}
switch ($options['output']) {
case 'file':
switch ($image_info[2]) {
case IMAGETYPE_JPEG:
return imagejpeg($rimage, $destination, $options['quality']);
case IMAGETYPE_PNG:
return imagepng($rimage, $destination, 0);
case IMAGETYPE_GIF:
return imagegif($rimage, $destination);
case IMAGETYPE_BMP:
return imagejpeg($rimage, $destination, $options['quality']);
default :
return false;
break;
}
return true;
break;
default :
return false;
break;
}
}
Example image causing the problem:
This image is successfully uploaded, but when I try to resize it, the resulting image is:
I call the function this way:
image_resize($ofile, $cfile, $width, $height, 'crop', 'jpeg');
Where $ofile is the original file, $cfile is the planned destination, $width is the desired width (90 in this case), $height is the desired height (90 in this case), 'crop' is the selected strategy and 'jpeg' is a certain $type value, which is unused in the function (as I have mentioned, I have inherited the code). Also, the only example where the problem could be reproduced is the attached image, which is a png, other png files are uploaded correctly, so I do not understand the cause of the issue and do not know how to solve it. Can anybody describe the cause of the problem? I have searched and experimented for a long while without achieving success.
i tried your "image_resize" function with your bird picture, and it works perfectly fine on my computer,
whether i set the source image to jpg or png, it works as expected :
However why not choosing "fit" instead of "crop" like so :
image_resize($ofile, $cfile, $width = 90, $height = 90, 'fit', 'jpeg');
Edit: based on other people's issues about getting black image after resizing PNG, this would be the correction:
function image_resize($source, $destination, $width, $height, $resizeMode='fit', $type = 'jpeg', $options = array()) {
$defaults = array(
'output' => 'file',
'isFile' => true,
'quality' => '100',
'preserveAnimation' => false,
'offsetTop' => 0,
'offsetLeft' => 0,
'offsetType' => 'percent'
);
foreach ($defaults as $k => $v) {
if (!isset($options[$k])) {
$options[$k] = $v;
}
}
if ($options['isFile']) {
$image_info = getimagesize($source);
$image = null;
switch ($image_info[2]) {
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($source);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($source);
break;
case IMAGETYPE_GIF:
$image = imagecreatefromgif($source);
break;
case IMAGETYPE_BMP:
$image = imagecreatefromwbmp($source);
break;
default :
return false;
}
} else {
$image = imagecreatefromstring($source);
}
//we have an image resource
$iwidth = imagesx($image);
$iheight = imagesy($image);
//determine ratios
$wratio = $width / $iwidth;
$hratio = $height / $iheight;
$mratio = min(array($wratio, $hratio));
$rimage = null;
switch ($resizeMode) {
case 'fit':
$rimage = imagecreatetruecolor($iwidth * $mratio, $iheight * $mratio);
imagealphablending( $rimage, false );
imagesavealpha( $rimage, true );
$image = imagecopyresampled($rimage, $image, 0, 0, 0, 0, $iwidth * $mratio, $iheight * $mratio, $iwidth, $iheight);
break;
case 'crop':
$rratio = $width / $height;
if ($rratio < 1) {
$nwidth = $iwidth;
$nheight = $iwidth * 1/$rratio;
if ($nheight>$iheight) {
$nwidth = $nwidth*$iheight/$nheight;
$nheight = $iheight;
}
} else {
$nwidth = $iheight*$rratio;
$nheight = $iheight;
if ($nwidth>$iwidth) {
$nheight = $nheight*$iwidth/$nwidth;
$nwidth = $iwidth;
}
}
switch ($options['offsetType']) {
case 'percent':
$sx = ($iwidth-$nwidth)*$options['offsetLeft']/100;
$sy = ($iheight-$nheight)*$options['offsetTop']/100;
break;
default :
return false;
}
$rimage = imagecreatetruecolor($width, $height);
imagealphablending( $rimage, false );
imagesavealpha( $rimage, true );
$image = imagecopyresampled($rimage, $image, 0, 0, $sx, $sy, $width, $height, $nwidth, $nheight);
break;
default :
return false;
break;
}
if (!is_writeable(dirname($destination))) {
throw new Exception(getcwd(). "/" .dirname($destination)." is not writeable");
}
switch ($options['output']) {
case 'file':
switch ($image_info[2]) {
case IMAGETYPE_JPEG:
return imagejpeg($rimage, $destination, $options['quality']);
case IMAGETYPE_PNG:
return imagepng($rimage, $destination, 0);
case IMAGETYPE_GIF:
return imagegif($rimage, $destination);
case IMAGETYPE_BMP:
return imagejpeg($rimage, $destination, $options['quality']);
default :
return false;
break;
}
return true;
break;
default :
return false;
break;
}
}

Creating a thumbnail from an uploaded image showing black background

I'm getting image thumbnail but background is black.i could not understand why it is happening.Though i found this script in stackoverflow.
Is it problem in gd library.i'm using xampp
makeThumbnails($upload_dir.$new_nm,227,182,$upload_dir_thumb.$new_nm);
function makeThumbnails($src,$w,$h,$dir)
{
$arr_image_details = getimagesize($src);
$original_width = $arr_image_details[0];
$original_height = $arr_image_details[1];
if ($original_width > $original_height) {
$new_width = $w;
$new_height = intval($original_height * $new_width / $original_width);
} else {
$new_height = $h;
$new_width = intval($original_width * $new_height / $original_height);
}
$dest_x = intval(($w - $new_width) / 2);
$dest_y = intval(($h - $new_height) / 2);
if ($arr_image_details[2] == 1) {
$imgt = "ImageGIF";
$imgcreatefrom = "ImageCreateFromGIF";
}
if ($arr_image_details[2] == 2) {
$imgt = "ImageJPEG";
$imgcreatefrom = "ImageCreateFromJPEG";
}
if ($arr_image_details[2] == 3) {
$imgt = "ImagePNG";
$imgcreatefrom = "ImageCreateFromPNG";
}
if ($imgt) {
$old_image = $imgcreatefrom($src);
$new_image = imagecreatetruecolor($w, $h);
imagecopyresized($new_image, $old_image, $dest_x, $dest_y, 0, 0, $new_width, $new_height, $original_width, $original_height);
$imgt($new_image, $dir);
}
I think You are doing wrong with $old_image = $imgcreatefrom($src)
What will return `$imgcreatefrom($src)` check it.
function makethumb($raw,$tw,$th,$DestFolder)
{
$arr_image_details = getimagesize($raw);
$CurWidth = $arr_image_details[0];
$CurHeight = $arr_image_details[1];
//print_r($arr_image_details);exit;
switch(strtolower($arr_image_details['mime']))
{
case 'image/png':
$CreatedImage = imagecreatefrompng($raw);
break;
case 'image/gif':
$CreatedImage = imagecreatefromgif($raw);
break;
case 'image/jpeg':
case 'image/pjpeg':
$CreatedImage = imagecreatefromjpeg($raw);
break;
default:
$processImage = false; //image format is not supported!
}
if($tw >=$th )
{
$MaxSize=$tw;
}else{
$MaxSize=$th;
}
$ImageScale = min($MaxSize/$CurWidth, $MaxSize/$CurHeight);
$NewWidth = ceil($ImageScale*$CurWidth);
$NewHeight = ceil($ImageScale*$CurHeight);
if($CurWidth < $NewWidth || $CurHeight < $NewHeight)
{
$NewWidth = $CurWidth;
$NewHeight = $CurHeight;
}
$NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
if(imagecopyresampled($NewCanves, $CreatedImage,0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight))
{
switch(strtolower($arr_image_details['mime']))
{
case 'image/png':
imagepng($NewCanves,$DestFolder);
break;
case 'image/gif':
imagegif($NewCanves,$DestFolder);
break;
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves,$DestFolder,90);
break;
default:
return false;
}
if(is_resource($NewCanves)) {
imagedestroy($NewCanves);
}
return true;
}
}

I am getting black background image while uploading png image with trransperent

I am getting black background image while uploading png image with transparent.Can u help me
My code is pasted here
$temp_image_path = $_FILES['img_recipe']['tmp_name'];
$temp_image_name = $_FILES['img_recipe']['name'];
$img_arr = getimagesize( $temp_image_path );
$ext = '';
switch($img_arr['mime'])
{
case 'image/gif': $ext = 'gif'; break;
case 'image/png': $ext = 'png'; break;
case 'image/jpeg': $ext = 'jpg'; break;
case 'image/pjpeg': $ext = 'jpg'; break;
case 'image/x-png': $ext = 'png'; break;
default: return false;
}
$temp_image_name = genToken( $len = 32, $md5 = true );
$temp_image_name .= "." . $ext;
$uploaded_image_path = UPLOADED_IMAGE_DESTINATION . $temp_image_name;
move_uploaded_file( $temp_image_path, $uploaded_image_path );
$recipe_image_path = RECIPE_IMAGE_DESTINATION . $temp_image_name;
$recipe_thumb_image_path = RECIPE_THUMB_IMAGE_DESTINATION . $temp_image_name;
$result_recipe = generate_image_resize( $uploaded_image_path, $recipe_image_path, RECIPE_MAX_WIDTH, RECIPE_MAX_HEIGHT );
$result_recipe_thumb = generate_image_resize( $uploaded_image_path, $recipe_thumb_image_path, RECIPE_THUMB_MAX_WIDTH, RECIPE_THUMB_MAX_HEIGHT );
unlink($uploaded_image_path);
function generate_image_resize( $source_image_path, $thumbnail_image_path, $width, $height )
{
list( $source_image_width, $source_image_height, $source_image_type ) = getimagesize( $source_image_path );
switch ( $source_image_type )
{
case IMAGETYPE_GIF:
$source_gd_image = imagecreatefromgif( $source_image_path );
break;
case IMAGETYPE_JPEG:
$source_gd_image = imagecreatefromjpeg( $source_image_path );
break;
case IMAGETYPE_PNG:
$source_gd_image = imagecreatefrompng( $source_image_path );
break;
}
if ( $source_gd_image === false ){ return false; }
$thumbnail_image_width = $width;
$thumbnail_image_height = $height;
$source_aspect_ratio = $source_image_width / $source_image_height;
$thumbnail_aspect_ratio = $thumbnail_image_width / $thumbnail_image_height;
if ( $source_image_width <= $thumbnail_image_width && $source_image_height <= $thumbnail_image_height )
{ $thumbnail_image_width = $source_image_width; $thumbnail_image_height = $source_image_height; }
elseif ( $thumbnail_aspect_ratio > $source_aspect_ratio )
{ $thumbnail_image_width = ( int ) ( $thumbnail_image_height * $source_aspect_ratio ); }
else
{ $thumbnail_image_height = ( int ) ( $thumbnail_image_width / $source_aspect_ratio ); }
$thumbnail_gd_image = imagecreatetruecolor( $thumbnail_image_width, $thumbnail_image_height );
imagecopyresampled( $thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height );
imagejpeg( $thumbnail_gd_image, $thumbnail_image_path, 90 );
imagedestroy( $source_gd_image );
imagedestroy( $thumbnail_gd_image );
return true;
}
Lets call source image $si, thumbnail $ti (so long...)
Enable transparency in loaded images :
imagealphablending($si, true);
Get the original transparent color of the loaded image :
$tc = imagecolortransparent($si);
$tc = ($tc != -1)? $tc = imagecolorsforindex($si, $tc):'hopeless';
This creates a black one:
$ti = imagecreatetruecolor( $ti_width, $ti_height );
Then after creating that:
if($tc !='hopeless'){ // do we have transparent color?
// calculate new transparent color
$tn = imagecolorallocate( $ti, $tc['red'], $tc['green'],$tc['blue']);
// we need it as index from now on
$tn = imagecolortransparent( $ti, $tn );
// fill target with transparent color
imagefill( $ti, 0,0, $tn);
// assign the transparent color in target
imagecolortransparent( $ti, $tn );
}
Now a resample may change color indexes on the original image. So artefacts will exist.
imagecopyresampled($ti,$si, 0,0,0,0,$ti_width,$ti_height,$si_width,$si_height );
Add Image Transparency Code Between these two lines:
$thumbnail_gd_image = imagecreatetruecolor( $thumbnail_image_width, $thumbnail_image_height );
imagecopyresampled( $thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0,$thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height );
SO new code will be:
$thumbnail_gd_image = imagecreatetruecolor( $thumbnail_image_width, $thumbnail_image_height );
// preserve transparency
if($ext = 'gif' || $ext = 'png')
{
imagecolortransparent($thumbnail_gd_image, imagecolorallocatealpha($thumbnail_gd_image, 0, 0, 0, 127));
imagealphablending($thumbnail_gd_image, false);
imagesavealpha($thumbnail_gd_image, true);
}
imagecopyresampled( $thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height );
I don't have an answer (I think #Ihson alreay gave it), but an advice.
Instead of :
switch ($img_arr['mime'])
{
case 'image/gif': $ext = 'gif';
break;
case 'image/png': $ext = 'png';
break;
case 'image/jpeg': $ext = 'jpg';
break;
case 'image/pjpeg': $ext = 'jpg';
break;
case 'image/x-png': $ext = 'png';
break;
default: return false;
}
// (...)
switch ($source_image_type)
{
case IMAGETYPE_GIF:
$source_gd_image = imagecreatefromgif($source_image_path);
break;
case IMAGETYPE_JPEG:
$source_gd_image = imagecreatefromjpeg($source_image_path);
break;
case IMAGETYPE_PNG:
$source_gd_image = imagecreatefrompng($source_image_path);
break;
}
I suggest you to use :
$string = file_get_contents($source_image_path);
$img = #imagecreatefromstring($string);
if ($img === false) {
// something went wrong...
} else {
// do what you want with $img
}
This is discusting to use # operator, but in this case, there is at least 2 reasons :
less code to write / to test / to maintain
mime type may be wrong (if I rename a .jpg as a .png).

image thumbnailer creating a black background

This is a pretty typical script to create a thumbnail. How come it keeps outputting a black canvas instead of the white I am trying to specify? Am I missing something totally obvious here?
<?php
$image = 'images/original/' . $_GET['image'];
$thumb = "images/thumbs/t_" . $_GET['image'];
$size = 220;
square_crop($image, $thumb, $size);
function square_crop($src_image, $dest_image, $thumb_size, $jpg_quality = 100)
{
// Get dimensions of existing image
$image = getimagesize($src_image);
// Check for valid dimensions
if( $image[0] <= 0 || $image[1] <= 0 ) return false;
// Determine format from MIME-Type
$image['format'] = strtolower(preg_replace('/^.*?\//', '', $image['mime']));
// Import image
switch( $image['format'] ) {
case 'jpg':
case 'jpeg':
$image_data = imagecreatefromjpeg($src_image);
break;
case 'png':
$image_data = imagecreatefrompng($src_image);
break;
case 'gif':
$image_data = imagecreatefromgif($src_image);
break;
default:
// Unsupported format
return false;
break;
}
// Verify import
if( $image_data == false ) return false;
// Calculate measurements
if( $image[0] & $image[1] ) {
// For landscape images
$x_offset = ($image[0] - $image[1]) / 2;
$y_offset = 0;
$square_size = $image[0] - ($x_offset * 2);
} else {
// For portrait and square images
$x_offset = 0;
$y_offset = ($image[1] - $image[0]) / 2;
$square_size = $image[1] - ($y_offset * 2);
}
//$im = imagecreatetruecolor(100, 100);
// sets background to red
//$red = imagecolorallocate($im, 255, 0, 0);
//imagefill($im, 0, 0, $red);
// Resize and crop
$canvas = imagecreatetruecolor($thumb_size, $thumb_size);
//make the background white
$white = imagecolorallocate($canvas, 255, 255, 255);
imagefill($canvas, 0,0, $white);
if( imagecopyresampled(
$canvas,
$image_data,
0,
0,
$x_offset,
$y_offset,
$thumb_size,
$thumb_size,
$square_size,
$square_size))
{
// Create thumbnail
switch( strtolower(preg_replace('/^.*\./', '', $dest_image)) ) {
case 'jpg':
case 'jpeg':
return imagejpeg($canvas, $dest_image, $jpg_quality);
break;
case 'png':
return imagepng($canvas, $dest_image);
break;
case 'gif':
return imagegif($canvas, $dest_image);
break;
default:
// Unsupported format
return false;
break;
}
} else {
return false;
}
}
?>

Black square at bottom on output thumbnail

I'm having a black area at my output imagecopyresized() thumbnail image.
My code:
function thumbImage($src){
/* thumb */
list($height, $width) = getimagesize($src);
$rel_difference_thumb = array('width'=>0, 'height'=>0);
if($width > 79) { $rel_difference_thumb['width'] = ($width-79)/79; }
if($height > 105) { $rel_difference_thumb['height'] = ($height-105)/105; }
asort($rel_difference_thumb);
$newwidth_thumb = $width/(1+end($rel_difference_thumb));
$newheight_thumb = $height/(1+end($rel_difference_thumb));
$newwidth_thumb = round($newwidth_thumb);
$newheight_thumb = round($newheight_thumb);
$jpeg_quality_thumb = 90;
$thumbloc = 'images/users/privAlbum/thumb/'.$USER . md5(uniqid()) . '.jpg';
switch(exif_imagetype($src)) {
case IMAGETYPE_GIF:
$img_r_thumb = imagecreatefromgif($src);
break;
case IMAGETYPE_JPEG:
$img_r_thumb = imagecreatefromjpeg($src);
break;
case IMAGETYPE_PNG:
$img_r_thumb = imagecreatefrompng($src);
break;
default:
echo json_encode(array('error' => 'Ingen bild!'));
exit(0);
break;
}
$dst_r_thumb = ImageCreateTrueColor( $newwidth_thumb, $newheight_thumb );
imagecopyresized($dst_r_thumb, $img_r_thumb, 0, 0, 0, 0, $newwidth_thumb , $newheight_thumb, $width, $height);
if( imagejpeg($dst_r_thumb,$thumbloc,$jpeg_quality_thumb) ) {
return true;
}
imagedestroy($img_r_thumb);
}
Why is this happening? How can I fix this?
list($height, $width) = getimagesize($src); should be list($width, $height) = getimagesize($src);
as said on the manual on getimagesize :
Returns an array with 7 elements:
Index 0 and 1 contains respectively
the width and the height of the image.

Categories