Related
I'm using Phalcon PHP Phalcon\Images\Adapter\GD and I have a problem with the PNG to JPG.
When I upload a PNG image on my server, I resize this image, I set the background color in white with the background method $myImage->background('#FFFFFF') but this background is now black... I don't understand why and after I split the extension .PNG and I add .JPG.
How can I do to set the background color image to white ?
Try this it worked for me :
function img_resize($file,$width=0,$height=0,$proportional=false,$output='file' ) {
if ( $height <= 0 && $width <= 0 ) return false;
# Setting defaults and meta
$info= getimagesize($file);
$image= '';
$final_width= 0;
$final_height= 0;
list($width_old, $height_old) = $info;
# Calculating proportionality
if ($proportional) {
if ($width == 0) $factor = $height/$height_old;
elseif ($height == 0) $factor = $width/$width_old;
else $factor = min( $width / $width_old, $height / $height_old );
$final_width = round( $width_old * $factor );
$final_height = round( $height_old * $factor );
}
else {
$final_width = ( $width <= 0 ) ? $width_old : $width;
$final_height = ( $height <= 0 ) ? $height_old : $height;
}
# Loading image to memory according to type
switch ( $info[2] ) {
case IMAGETYPE_GIF: $image = imagecreatefromgif($file); break;
case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($file); break;
case IMAGETYPE_PNG: $image = imagecreatefrompng($file); break;
default: return false;
}
# This is the resizing/resampling/transparency-preserving magic
$image_resized = imagecreatetruecolor( $final_width, $final_height );
if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
$transparency = imagecolortransparent($image);
if ($transparency >= 0) {
$transparent_color = imagecolorsforindex($image, $trnprt_indx);
$transparency = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
imagefill($image_resized, 0, 0, $transparency);
imagecolortransparent($image_resized, $transparency);
}
elseif ($info[2] == IMAGETYPE_PNG) {
imagealphablending($image_resized, false);
$color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
imagefill($image_resized, 0, 0, $color);
imagesavealpha($image_resized, true);
}
}
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
# Preparing a method of providing result
switch ( strtolower($output) ) {
case 'browser':
$mime = image_type_to_mime_type($info[2]);
header("Content-type: $mime");
$output = NULL;
break;
case 'file':
$output = $file;
break;
case 'return':
return $image_resized;
break;
default:
break;
}
# Writing image according to type to the output destination
switch ( $info[2] ) {
case IMAGETYPE_GIF: imagegif($image_resized, $output); break;
case IMAGETYPE_JPEG: imagejpeg($image_resized, $output); break;
case IMAGETYPE_PNG: imagepng($image_resized, $output); break;
default: return false;
}
return true;
}
Call function like below :
$image = new Phalcon\Image\Adapter\GD(YOUR_IMAGE_PATH);
$a=img_resize($image->getRealpath(),$image->getWidth(),$image->getHeight());
Some code might not necessary for you.
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;
}
}
I want to create thumbnails from image, but fixed sizes - 310px width, 217px height.
My code:
list($width, $height) = getimagesize($filename);
$width = 310;
$height = 217;
$new_width = 310;
$new_height = 217;
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);......
How can I make that? What is the formula?
function resizeImage($url, $width, $height, $url_out, $keep_ratio = false){
if($height <= 0 && $width <= 0)
return false;
else{
copy($url, $url_out);
$info = getimagesize($url);
$image = '';
$final_width = 0;
$final_height = 0;
list($width_old, $height_old) = $info;
if($keep_ratio){
if($width == 0)
$factor = $height/$height_old;
elseif($height == 0)
$factor = $width/$width_old;
else
$factor = min($width / $width_old, $height / $height_old);
$final_width = round( $width_old * $factor );
$final_height = round( $height_old * $factor );
}
else{
$final_width = ($width <= 0) ? $width_old : $width;
$final_height = ($height <= 0) ? $height_old : $height;
}
switch($info[2]){
case IMAGETYPE_GIF:
$image = imagecreatefromgif($url_out);
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($url_out);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($url_out);
break;
default:
return false;
}
$image_resized = imagecreatetruecolor($final_width, $final_height);
if($info[2] == IMAGETYPE_GIF || $info[2] == IMAGETYPE_PNG){
$transparency = imagecolortransparent($image);
if($transparency >= 0){
$transparent_color = imagecolorsforindex($image, $trnprt_indx);
$transparency = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
imagefill($image_resized, 0, 0, $transparency);
imagecolortransparent($image_resized, $transparency);
}
elseif($info[2] == IMAGETYPE_PNG){
imagealphablending($image_resized, false);
$color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
imagefill($image_resized, 0, 0, $color);
imagesavealpha($image_resized, true);
}
}
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
switch($info[2]){
case IMAGETYPE_GIF:
imagegif($image_resized, $url_out);
break;
case IMAGETYPE_JPEG:
imagejpeg($image_resized, $url_out);
break;
case IMAGETYPE_PNG:
imagepng($image_resized, $url_out);
break;
default:
return false;
}
imagedestroy($image_resized);
return true;
}
}
//just call the function, and change the image name
$url="desert09.jpg";
resizeImage($url, 310, 217,"output.jpg", $keep_ratio = false);
I have this code (i don't remember where i got it) and i have edited it a little. I hope it can help you, this create a fixed square size thumb:
/** Create a square cropped thumb **/
function createSquareCroppedThumb($path , $thumbPath, $thumbSize = 100 ){
global $max_width, $max_height;
/* Set Filenames */
$srcFile = $path;
$thumbFile = $thumbPath;
/* Determine the File Type */
$type = pathinfo($path, PATHINFO_EXTENSION);
/* Create the Source Image */
switch( $type ){
case 'jpg' : case 'jpeg' :
$src = imagecreatefromjpeg( $srcFile ); break;
case 'png' :
$src = imagecreatefrompng( $srcFile ); break;
case 'gif' :
$src = imagecreatefromgif( $srcFile ); break;
}
/* Determine the Image Dimensions */
$oldW = imagesx( $src );
$oldH = imagesy( $src );
$minValue = $oldH > $oldW ? $oldW : $oldH;
/* Create the New Image */
$new = imagecreatetruecolor( $thumbSize , $thumbSize );
/* Transcribe the Source Image into the New (Square) Image */
imagecopyresampled($new , $src , 0 , 0 , ($oldW / 2) - ($minValue /2) , ($oldH / 2) - ($minValue /2) , $thumbSize , $thumbSize , $minValue , $minValue );
switch( $type ){
case 'jpg' : case 'jpeg' :
$src = imagejpeg( $new , $thumbFile ); break;
case 'png' :
$src = imagepng( $new , $thumbFile ); break;
case 'gif' :
$src = imagegif( $new , $thumbFile ); break;
}
imagedestroy( $new );
}
I have this PHP code
if (!isset($_POST["imageSource"])) {
$_POST["imageSource"] = "test.jpg";
}
list($width, $height) = getimagesize($_POST["imageSource"]);
$viewPortW = $_POST["viewPortW"];
$viewPortH = $_POST["viewPortH"];
$pWidth = $_POST["imageW"];
$pHeight = $_POST["imageH"];
$ext = end(explode(".",$_POST["imageSource"]));
$function = returnCorrectFunction($ext);
$image = $function($_POST["imageSource"]);
$width = imagesx($image);
$height = imagesy($image);
// Resample
$image_p = imagecreatetruecolor($pWidth, $pHeight);
setTransparency($image,$image_p,$ext);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $pWidth, $pHeight, $width, $height);
imagedestroy($image);
$widthR = imagesx($image_p);
$hegihtR = imagesy($image_p);
$selectorX = $_POST["selectorX"];
$selectorY = $_POST["selectorY"];
if($_POST["imageRotate"]){
$angle = 360 - $_POST["imageRotate"];
$image_p = imagerotate($image_p,$angle,0);
$pWidth = imagesx($image_p);
$pHeight = imagesy($image_p);
//print $pWidth."---".$pHeight;
$diffW = abs($pWidth - $widthR) / 2;
$diffH = abs($pHeight - $hegihtR) / 2;
$_POST["imageX"] = ($pWidth > $widthR ? $_POST["imageX"] - $diffW : $_POST["imageX"] + $diffW);
$_POST["imageY"] = ($pHeight > $hegihtR ? $_POST["imageY"] - $diffH : $_POST["imageY"] + $diffH);
}
$dst_x = $src_x = $dst_y = $src_y = 0;
if($_POST["imageX"] > 0){
$dst_x = abs($_POST["imageX"]);
}else{
$src_x = abs($_POST["imageX"]);
}
if($_POST["imageY"] > 0){
$dst_y = abs($_POST["imageY"]);
}else{
$src_y = abs($_POST["imageY"]);
}
$viewport = imagecreatetruecolor($_POST["viewPortW"],$_POST["viewPortH"]);
setTransparency($image_p,$viewport,$ext);
imagecopy($viewport, $image_p, $dst_x, $dst_y, $src_x, $src_y, $pWidth, $pHeight);
imagedestroy($image_p);
$selector = imagecreatetruecolor($_POST["selectorW"], $_POST["selectorH"]);
setTransparency($viewport,$selector,$ext);
imagecopy($selector, $viewport, 0, 0, $selectorX, $selectorY, $_POST["viewPortW"], $_POST["viewPortH"]);
if (isset($_GET["destino"]) && !empty($_GET["destino"])) {
$file = "../../".$_GET["destino"];
} else {
$file = "test".time().".".$ext;
}
parseImage($ext,$selector,$file);
imagedestroy($viewport);
//Return value
echo $file;
/* Functions */
function determineImageScale($sourceWidth, $sourceHeight, $targetWidth, $targetHeight) {
$scalex = $targetWidth / $sourceWidth;
$scaley = $targetHeight / $sourceHeight;
return min($scalex, $scaley);
}
function returnCorrectFunction($ext){
$function = "";
switch($ext){
case "png":
$function = "imagecreatefrompng";
break;
case "jpeg":
$function = "imagecreatefromjpeg";
break;
case "jpg":
$function = "imagecreatefromjpeg";
break;
case "gif":
$function = "imagecreatefromgif";
break;
}
return $function;
}
function parseImage($ext,$img,$file = null){
switch($ext){
case "png":
imagepng($img,($file != null ? $file : ''));
break;
case "jpeg":
imagejpeg($img,($file ? $file : ''),100);
break;
case "jpg":
imagejpeg($img,($file ? $file : ''),100);
break;
case "gif":
imagegif($img,($file ? $file : ''));
break;
}
}
function setTransparency($imgSrc,$imgDest,$ext){
if($ext == "png" || $ext == "gif"){
$trnprt_indx = imagecolortransparent($imgSrc);
// If we have a specific transparent color
if ($trnprt_indx >= 0) {
// Get the original image's transparent color's RGB values
$trnprt_color = imagecolorsforindex($imgSrc, $trnprt_indx);
// Allocate the same color in the new image resource
$trnprt_indx = imagecolorallocate($imgDest, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
// Completely fill the background of the new image with allocated color.
imagefill($imgDest, 0, 0, $trnprt_indx);
// Set the background color for new image to transparent
imagecolortransparent($imgDest, $trnprt_indx);
}
// Always make a transparent background color for PNGs that don't have one allocated already
elseif ($ext == "png") {
// Turn off transparency blending (temporarily)
imagealphablending($imgDest, true);
// Create a new transparent color for image
$color = imagecolorallocatealpha($imgDest, 0, 0, 0, 127);
// Completely fill the background of the new image with allocated color.
imagefill($imgDest, 0, 0, $color);
// Restore transparency blending
imagesavealpha($imgDest, true);
}
}
}
Comes Cropzoom tool
Currently I generated JPG images with black background color.
I need to modify it so you can change (from the code) background color of the generated image in JPG format
All other things are working properly.
I have solved the problem.
I used this to give background color on all parts that I considered necessary
$backgroundColor = "FF0000"; // Red, for example
$backgroundColor= '0x'.$backgroundColor;
imagefilledrectangle($FinalImage, 0 , 0 , $width , $height , ($backgroundColor*1));
The full code
// Set the Background color
$background_color = "FFFFFF";
$background_color = "0x".$background_color;
if (!isset($_POST["imageSource"])) {
$_POST["imageSource"] = "test.jpg";
}
list($width, $height) = getimagesize($_POST["imageSource"]);
$viewPortW = $_POST["viewPortW"];
$viewPortH = $_POST["viewPortH"];
$pWidth = $_POST["imageW"];
$pHeight = $_POST["imageH"];
$ext = end(explode(".",$_POST["imageSource"]));
$function = returnCorrectFunction($ext);
$image = $function($_POST["imageSource"]);
$width = imagesx($image);
$height = imagesy($image);
// Modified to set background color
$image_p = imagecreatetruecolor($pWidth, $pHeight);
// ****************************************************************
// ********** NEXT LINE IS NEW TO SET BACKGROUND COLOR **************
// ****************************************************************
imagefilledrectangle($viewport, 0, 0, $pWidth, $pHeight, ($background_color * 1));
setTransparency($image,$image_p,$ext);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $pWidth, $pHeight, $width, $height);
imagedestroy($image);
$widthR = imagesx($image_p);
$hegihtR = imagesy($image_p);
$selectorX = $_POST["selectorX"];
$selectorY = $_POST["selectorY"];
if($_POST["imageRotate"]){
$angle = 360 - $_POST["imageRotate"];
$image_p = imagerotate($image_p,$angle,0);
$pWidth = imagesx($image_p);
$pHeight = imagesy($image_p);
//print $pWidth."---".$pHeight;
$diffW = abs($pWidth - $widthR) / 2;
$diffH = abs($pHeight - $hegihtR) / 2;
$_POST["imageX"] = ($pWidth > $widthR ? $_POST["imageX"] - $diffW : $_POST["imageX"] + $diffW);
$_POST["imageY"] = ($pHeight > $hegihtR ? $_POST["imageY"] - $diffH : $_POST["imageY"] + $diffH);
}
$dst_x = $src_x = $dst_y = $src_y = 0;
if($_POST["imageX"] > 0){
$dst_x = abs($_POST["imageX"]);
}else{
$src_x = abs($_POST["imageX"]);
}
if($_POST["imageY"] > 0){
$dst_y = abs($_POST["imageY"]);
}else{
$src_y = abs($_POST["imageY"]);
}
$viewport = imagecreatetruecolor($_POST["viewPortW"],$_POST["viewPortH"]);
// ****************************************************************
// ********** NEXT LINE IS NEW TO SET BACKGROUND COLOR **************
// ****************************************************************
imagefilledrectangle($viewport, 0, 0, $_POST["viewPortW"], $_POST["viewPortH"], ($background_color * 1));
setTransparency($image_p,$viewport,$ext);
imagecopy($viewport, $image_p, $dst_x, $dst_y, $src_x, $src_y, $pWidth, $pHeight);
imagedestroy($image_p);
$selector = imagecreatetruecolor($_POST["selectorW"], $_POST["selectorH"]);
setTransparency($viewport,$selector,$ext);
imagecopy($selector, $viewport, 0, 0, $selectorX, $selectorY, $_POST["viewPortW"], $_POST["viewPortH"]);
if (isset($_GET["destino"]) && !empty($_GET["destino"])) {
$file = "../../".$_GET["destino"];
} else {
$file = "test".time().".".$ext;
}
parseImage($ext,$selector,$file);
imagedestroy($viewport);
//Return value
echo $file;
/* Functions */
function determineImageScale($sourceWidth, $sourceHeight, $targetWidth, $targetHeight) {
$scalex = $targetWidth / $sourceWidth;
$scaley = $targetHeight / $sourceHeight;
return min($scalex, $scaley);
}
function returnCorrectFunction($ext){
$function = "";
switch($ext){
case "png":
$function = "imagecreatefrompng";
break;
case "jpeg":
$function = "imagecreatefromjpeg";
break;
case "jpg":
$function = "imagecreatefromjpeg";
break;
case "gif":
$function = "imagecreatefromgif";
break;
}
return $function;
}
function parseImage($ext,$img,$file = null){
switch($ext){
case "png":
imagepng($img,($file != null ? $file : ''));
break;
case "jpeg":
imagejpeg($img,($file ? $file : ''),100);
break;
case "jpg":
imagejpeg($img,($file ? $file : ''),100);
break;
case "gif":
imagegif($img,($file ? $file : ''));
break;
}
}
function setTransparency($imgSrc,$imgDest,$ext){
if($ext == "png" || $ext == "gif"){
$trnprt_indx = imagecolortransparent($imgSrc);
// If we have a specific transparent color
if ($trnprt_indx >= 0) {
// Get the original image's transparent color's RGB values
$trnprt_color = imagecolorsforindex($imgSrc, $trnprt_indx);
// Allocate the same color in the new image resource
$trnprt_indx = imagecolorallocate($imgDest, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
// Completely fill the background of the new image with allocated color.
imagefill($imgDest, 0, 0, $trnprt_indx);
// Set the background color for new image to transparent
imagecolortransparent($imgDest, $trnprt_indx);
}
// Always make a transparent background color for PNGs that don't have one allocated already
elseif ($ext == "png") {
// Turn off transparency blending (temporarily)
imagealphablending($imgDest, true);
// Create a new transparent color for image
$color = imagecolorallocatealpha($imgDest, 0, 0, 0, 127);
// Completely fill the background of the new image with allocated color.
imagefill($imgDest, 0, 0, $color);
// Restore transparency blending
imagesavealpha($imgDest, true);
}
}
}
It is modified after "NEXT LINE IS NEW TO SET BACKGROUND COLOR" and all the tests I've done, it works correctly
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.