Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I´m using this function to add a watermark on the top left corner of an image, jpeg or png.
I need the Switch and Case to differentiate between the png and the jpeg to process each image type separately.
Each time I run the function, images as edited perfectly, but nevertheless the debug shows that the images are being processed both as .png and .jpg therefore emitting a problem when the image is of the wrong type.
function addWatermark($path)
{
$public_file_path = dirname(__FILE__);
$font = $public_file_path . '/src/fonts/Roboto-Light.ttf';
// get fiel type for switch
$size = getimagesize($path);
// get $from name
$path_parts = pathinfo($path);
$text= $path_parts['filename'];
switch($path_parts['extension'])
{
case "jpeg" or "jpg":
// Copy and resample the imag
list($width, $height) = getimagesize($path);
$image_p = imagecreatefromjpeg($path);
// Prepare font size and colors
$text_color = imagecolorallocate($image_p, 0, 0, 0);
$bg_color = imagecolorallocate($image_p, 255, 255, 255);
$font_size = 17;
// Set the offset x and y for the text position
$offset_x = 0;
$offset_y = 20;
// Get the size of the text area
$dims = imagettfbbox($font_size, 0, $font, $text);
$text_width = $dims[4] - $dims[6] + $offset_x;
$text_height = $dims[3] - $dims[5] + $offset_y;
// Add text background
imagefilledrectangle($image_p, 0, 0, $text_width, $text_height, $bg_color);
// Add text
imagettftext($image_p, $font_size, 0, $offset_x, $offset_y, $text_color, $font, $text);
// Save the picture
imagejpeg($image_p, $path, 90);
// Clear
imagedestroy($image_p);
case "png":
$im = imagecreatefrompng($path);
imagesavealpha($im, true); // important to keep the png's transparency
$text_color = imagecolorallocate($im, 0, 0, 0);
$width = imagesx($im); // the width of the image
$height = imagesy($im);; // the heighst of the image
$font_size = 15; // font size
$box_color = imagecolorallocate($im, 255, 255, 255);
// Set the offset x and y for the text position
$offset_x = 0;
$offset_y = 20;
$dims = imagettfbbox($font_size, 0, $font, $text);
$text_width = $dims[4] - $dims[6] + $offset_x;
$text_height = $dims[3] - $dims[5] + $offset_y;
// Add text background
imagefilledrectangle($im, 0, 0, $text_width, $text_height, $box_color);
// Add text
imagettftext($im, $font_size, 0, $offset_x, $offset_y, $text_color, $font,$text);
imagepng($im, $path, 0);
imagedestroy($im);
case "mp4";
break;
}
// hasta aca
}
I don't believe you can use an or in the case statement. From this example, everything will match the first case: http://sandbox.onlinephpfunctions.com/code/8edfd01d386e9f7bb56975b9b1ecb9dee4669494
Your code should be:
switch($path_parts['extension'])
{
case "jpeg":
case "jpg":
// Your code
break;
case "png":
// Your code
break;
case "mp4";
break;
}
You will need to add break; to your case statements.
switch($path_parts['extension'])
{
case "jpeg":
case "jpg":
// Your code
break;
case "png":
// Your code
break;
case "mp4";
break;
}
So it looks like you may be missing break statments and I'm not sure if case('jpg') or case('jpeg') will work. You can use this alternative:
switch(var) {
case('jpg'):
case('jpeg'):
break;
case('png'):
break;
default:
break;
}
Related
I created a function that takes images, resizes them to fit at least one dimension perfectly onto a canvas and finally crops out excess image content.
Process:
Take a 640x400 image
Scale image fit into 450x450 canvas (image is now 450x281)
Crop out excess data if any
The problem I'm encountering currently involves images that are too small for the newly created thumbnail (such as the one provided in the example above). I expected the output to either have a white background(JPGs) or a transparent background(PNGs). However, despite my best efforts, this tends to be the resulting mess:
As can be observed, the image has a black background in place of a transparent/white background. I am completely stumped on how to correct this flaw.
A copy of my code:
function create_thumbnail($path, $saveto, $width, $height) {
ini_set('memory_limit', '128M'); //Unlocking more memory for download
$info = getimagesize($path);
$rate = $info[0]/$info[1];
// Determine image size/position
if ($info[0] < $width || $info[1] < $height) {
// Image is too small
if ($info[0] < $width && $info[1] < $height) {
// Both width and height too small
$nw = $info[0];
$nh = $info[1];
} else if ($info[0] < $width) {
// Width is too small
$nw = ($info[0]*$height)/$info[1];
$nh = $height;
} else if ($info[1] < $height) {
// Height is too small
$nw = $width;
$nh = ($info[1]*$width)/$info[0];
}
} else {
// Image fits
if (($width/$height) > $rate) {
$nw = $width;
$nh = $width/$rate;
} else {
$nw = $height*$rate;
$nh = $height;
}
}
$nw = round($nw);
$nh = round($nh);
$x_mid = round($nw/2);
$y_mid = round($nh/2);
switch($info[2]) {
case IMAGETYPE_PNG :
$src = imagecreatefrompng($path);
break;
case IMAGETYPE_JPEG :
$src = imagecreatefromjpeg($path);
break;
case IMAGETYPE_GIF :
$src = imagecreatefromgif($path);
break;
default :
return false;
}
// Create image
$proc = imagecreatetruecolor($nw, $nh);
$clr = imagecolorallocate($proc, 255, 255, 255);
imagefill($proc, 0, 0, $clr);
imagecopyresampled($proc, $src, 0, 0, 0, 0, $nw, $nh, $info[0], $info[1]);
$thmb = imagecreatetruecolor($width, $height);
$clr = imagecolorallocate($thmb, 255, 255, 255);
imagefill($thmb, 0, 0, $clr);
imagecopyresampled($thmb, $proc, 0, 0, ($x_mid-($width/2)), ($y_mid-($height/2)), $width, $height, $width, $height);
if ($info[2] == IMAGETYPE_PNG || $info[2] == IMAGETYPE_GIF) {
$trnprt_idx = imagecolortransparent($src);
if ($trnprt_idx >= 0) {
// Attempt to forcefully correct transparencies using original image's color index
$trnprt_clr = imagecolorsforindex($src, $trnprt_idx);
$trnprt_idx = imagecolorallocate($thmb, $trnprt_clr['red'], $trnprt_clr['green'], $trnprt_clr['blue']);
imagefill($thmb, 0, 0, $trnprt_idx);
imagecolortransparent($thmb, $trnprt_idx);
imagealphablending($thmb, false);
imagesavealpha($thmb, true);
} else if ($info[2] == IMAGETYPE_PNG) {
// Attempt to forcefully correct transparencies by shutting down blending
$clr = imagecolorallocatealpha($thmb, 0, 0, 0, 127);
imagefill($thmb, 0, 0, $clr);
imagealphablending($thmb, false);
imagesavealpha($thmb, true);
}
}
switch($info[2]) {
case IMAGETYPE_PNG :
imagepng($thmb, $saveto);
break;
case IMAGETYPE_JPEG :
imagejpeg($thmb, $saveto, 100);
break;
case IMAGETYPE_GIF :
imagegif($thmb, $saveto);
break;
default :
return false;
}
return true;
} //End of create_thumbnail()
I have attempted to correct the transparency/coloring (as visible in my code), but it only affects one side of the image. Everything I have tried has either resulting in one side having a transparent/white background or both sides being completely black.
After a long time spent playing around trying to figure out what exactly was going on and breaking, I have found the solution.
The problem is here:
$proc = imagecreatetruecolor($nw, $nh);
$clr = imagecolorallocate($proc, 255, 255, 255);
imagefill($proc, 0, 0, $clr);
imagecopyresampled($proc, $src, 0, 0, 0, 0, $nw, $nh, $info[0], $info[1]);
$thmb = imagecreatetruecolor($width, $height);
$clr = imagecolorallocate($thmb, 255, 255, 255);
imagefill($thmb, 0, 0, $clr);
imagecopyresampled($thmb, $proc, 0, 0, ($x_mid-($width/2)), ($y_mid-($height/2)), $width, $height, $width, $height);
The newly created image $proc was not transferring it's transparency over to $thmb when I was doing imagecopyresampled from one to the other. The solution I found was to either skip creating/using $thmb altogether, or to save $proc as a png/gif first, then use that saved image for imagecopyresample.
I am using PHP and GD to crop and output an image with the code below. it works fine but when i pass a transparent PNG into it i get a black background generated. How can i stop this?
//setup
switch ($source_type) {
case IMAGETYPE_JPEG: $source = imagecreatefromjpeg($img_path); break;
case IMAGETYPE_PNG: $source = imagecreatefrompng($img_path); break;
}
// setup cropped destination
$cropped = imagecreatetruecolor($cropped_width, $cropped_height);
// create cropped image
$x = (($source_width / 100) * IMAGE_X) - ($cropped_width / 2);
$y = (($source_height / 100) * IMAGE_Y) - ($cropped_height / 2);
imagecopy(
$cropped,
$source,
0, 0,
$x, $y,
$cropped_width, $cropped_height
);
// output inc header
header('Content-type: image/jpeg');
imagejpeg($cropped);
It should be something along the lines of:
switch ($source_type)
{
case IMAGETYPE_PNG:
$background = imagecolorallocate($source, 0, 0, 0);
// remove the black
imagecolortransparent($source, $background);
// turn off alpha blending
imagealphablending($source, false);
imagesavealpha($source, true);
break;
}
There is a similar question here
I have tried many solutions and the truth and got to the point where you do not know what else to do.
The following image is a PNG ("cover.png"):
As you will have a blank oval really is completely transparent. With PHP I'm trying to fuse it to this picture ("lapiz.jpg"):
However, despite how much I've tried to not get the clear space of the first image is transparent and instead goes completely blank, covering the image that should melt.
For now this is my code:
$img_user = 'fotos/lapiz.jpg';
$img_user_type = getImageInfo($img_user,'type');
$posX = 404;
$posY = 2;
$width = getImageInfo($img_user,'width');
$height = getImageInfo($img_user,'height');
$stamp = 'fotos/cover.png';
switch($img_user_type)
{
case 'jpeg':
$img_user_create = imagecreatefromjpeg($img_user);
break;
case 'gif':
$img_user_create = imagecreatefromgif($img_user);
break;
case 'png':
$img_user_create = imagecreatefrompng($img_user);
break;
}
$im = imagecreatefrompng($stamp);
imagealphablending($im, false);
imagesavealpha($im, true);
imagecolortransparent($im, imagecolorallocate($im, 255, 255, 255));
imagecopymerge($img_user_create, $im, $posX, $posY, 0, 0, $width, $height, 100);
header('Content-Type: image/png');
imagepng($im);
ImageDestroy($im);
ImageDestroy($img_user_create);
What I can be doing wrong?
junihh resolved this using the imagemagick library and the code below:
$img1 = new Imagick('fotos/lapiz.jpg');
$img2 = new Imagick('fotos/cover.png');
$posX = 404;
$posY = 2;
$img2->compositeImage( $img1, imagick::COMPOSITE_DSTOVER, $posX, $posY );
header('Content-type: image/png');
echo($img2);
I am trying to allow the uploading of transparent profile pictures (PNGs and GIFs) on my site because it is sometimes quite annoying for the user to upload a transparent profile picture and the transparent areas become black. The problem is that the transparency is still being lost even after using the imagealpha*() functions.
I do realize that there are other questions about this, but the answers on them aren't working for me.
Here is my code:
// [...]
switch(strtolower($_FILES['picture']['type'])) {
case 'image/jpeg':
$image = imagecreatefromjpeg($_FILES['picture']['tmp_name']);
break;
case 'image/png':
$image = imagecreatefrompng($_FILES['picture']['tmp_name']);
break;
case 'image/gif':
$image = imagecreatefromgif($_FILES['picture']['tmp_name']);
break;
default:
msg('Sorry, but the type of file that you selected is not allowed. We only allow JPEG, PNG, and GIF.','error');
header("Location: /settings/profile");
exit;
}
// Target dimensions
$max_width = 143;
$max_height = 143;
// Get current dimensions
$old_width = imagesx($image);
$old_height = imagesy($image);
// Calculate the scaling we need to do to fit the image inside our frame
$scale = min($max_width/$old_width, $max_height/$old_height);
// Get the new dimensions
$new_width = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);
// Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);
// Resize old image into new
imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
$file_name = 'avatar_'.randString(20).mt_rand(111,999).'.'.str_replace('image/','',$_FILES['picture']['type']);
switch(strtolower($_FILES['picture']['type'])) {
case 'image/jpeg':
$img = imagejpeg($new, 'user/uploads/'.$file_name, 95);
break;
case 'image/png':
imagealphablending($new, false);
imagesavealpha($new, true);
$img = imagepng($new, 'user/uploads/'.$file_name, 95);
break;
case 'image/gif':
imagealphablending($new, false);
imagesavealpha($new, true);
$img = imagegif($new, 'user/uploads/'.$file_name);
break;
}
imagedestroy($image);
imagedestroy($new);
if($img) {
$dbUpdate = mysql_query("UPDATE users SET user_pic = '$file_name' WHERE uid = $userid");
}
if($img && $dbUpdate) {
msg("Your profile picture has been changed successfully.","success");
header("Location: /settings/profile");
exit;
}
// [...]
I tried uploading this GIF just for testing:
But it lost its transparency after it was uploaded:
I am trying to keep the transparency information with it, but it doesn't seem to be working. Am I not doing something right?
Thanks in advance.
Create a transparent color and fill the $new image with that color before the copy. If you don't do that, the background color of the new image will default to black.
$new = imagecreatetruecolor($new_width, $new_height);
$transparent = imagecolorallocatealpha($new, 0, 0, 0, 127);
imagefill($new, 0, 0, $transparent);
imagealphablending($new, true);
You can also check this question
I make thumbnails using PHP and GD library but my code turn png transparency into a solid black color, Is there a solution to improve my code?
this is my php thumbnail maker code:
function cropImage($nw, $nh, $source, $stype, $dest) {
$size = getimagesize($source);
$w = $size[0];
$h = $size[1];
switch($stype) {
case 'gif':
$simg = imagecreatefromgif($source);
break;
case 'jpg':
$simg = imagecreatefromjpeg($source);
break;
case 'png':
$simg = imagecreatefrompng($source);
break;
}
$dimg = imagecreatetruecolor($nw, $nh);
$wm = $w/$nw;
$hm = $h/$nh;
$h_height = $nh/2;
$w_height = $nw/2;
if($w> $h) {
$adjusted_width = $w / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
} elseif(($w <$h) || ($w == $h)) {
$adjusted_height = $h / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;
imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
} else {
imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
}
imagejpeg($dimg,$dest,100);
}
Thank you
After imagecreatetruecolor():
<?php
// ... Before imagecreatetruecolor()
$dimg = imagecreatetruecolor($width_new, $height_new); // png ?: gif
// start changes
switch ($stype) {
case 'gif':
case 'png':
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($dimg , 0, 0, 0);
// removing the black from the placeholder
imagecolortransparent($dimg, $background);
// turning off alpha blending (to ensure alpha channel information
// is preserved, rather than removed (blending with the rest of the
// image in the form of black))
imagealphablending($dimg, false);
// turning on alpha channel information saving (to ensure the full range
// of transparency is preserved)
imagesavealpha($dimg, true);
break;
default:
break;
}
// end changes
$wm = $w/$nw;
$hm = $h/$nh;
// ...
if the code of the correct answer don't work try this :
//After imagecreatetruecolor():
$white = imagecolorallocate($dimg, 255, 255, 255);
imagefill($dimg,0,0,$white);
The order of operations is important. for .gif images i found that i needed to copy resized image first, then assign the black background as transparent. for PNGs I found the code below resized images and kept the transparency backgrounds.
also, this code worked for me...
$resized_image = imagecreatetruecolor($target_width, $target_height);
switch ( $asset->a_mime_type )
{
case 'image/jpeg':
imagecopyresampled($resized_image, $source, 0, 0, 0, 0, $target_width, $target_height, $asset->a_image_width, $asset->a_image_height);
$r = imagejpeg($resized_image,$file_name);
break;
case 'image/png':
imagealphablending($resized_image, FALSE);
imagesavealpha($resized_image, TRUE);
imagecopyresampled($resized_image, $source, 0, 0, 0, 0, $target_width, $target_height, $asset->a_image_width, $asset->a_image_height);
$r = #imagepng($resized_image,$file_name);
break;
case 'image/gif':
imagecopyresampled($resized_image, $source, 0, 0, 0, 0, $target_width, $target_height, $asset->a_image_width, $asset->a_image_height);
$background = imagecolorallocate($resized_image, 0, 0, 0);
imagecolortransparent($resized_image, $background);
$r = #imagegif($resized_image,$file_name);
break;
}
Sometimes if the .jpg image got small errors inside (you cannot see that), all transparent pixels turn to black color.Try to use:
ini_set('gd.jpeg_ignore_warning', 1);
Just add this line :
imagesavealpha($image, true);
before
imagepng($image)
Here is my total test code. It works for me
$imageFileType = pathinfo($_FILES["image"]["name"], PATHINFO_EXTENSION);
$filename = 'test.' . $imageFileType;
move_uploaded_file($_FILES["image"]["tmp_name"], $filename);
$source_image = imagecreatefromjpeg($filename);
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);
$dest_imagex = 400;
$dest_imagey = 600;
$dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0, $dest_imagex, $dest_imagey, $source_imagex, $source_imagey);
imagesavealpha($dest_image, true);
$trans_colour = imagecolorallocatealpha($dest_image, 0, 0, 0, 127);
imagefill($dest_image, 0, 0, $trans_colour);
imagepng($dest_image,"test1.png",1);
change rgb to 255, it will give you transparent image, rather then black.
<?php
switch ($stype)
{
case "png":
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($simage, 255, 255, 255);
// removing the black from the placeholder
imagecolortransparent($simage, $background);
// turning off alpha blending (to ensure alpha channel information is preserved, rather than removed (blending with the rest of the image in the form of black))
imagealphablending($simage, false);
// turning on alpha channel information saving (to ensure the full range of transparency is preserved)
imagesavealpha($simage, true);
break;
case "gif":
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($simage, 255, 255, 255);
// removing the black from the placeholder
imagecolortransparent($simage, $background);
break;
}
?>
Some of the above made the black parts of the image turn white, while some didn't work at all. This, however, worked for me https://github.com/claviska/SimpleImage/issues/28