I´m trying to rotate a PNG image with PHP. The image rotates but a black background appears.
This is my code:
$image = $_GET['image'];
$degrees = $_GET['degrees'];
header('Content-type: image/png');
$source = imagecreatefrompng($image) ;
$rotate = imagerotate($source, $degrees, 0);
imagesavealpha($rotate, TRUE);
imagepng($rotate);
return rotate;
also add the line
imagealphablending($rotate, true);
before
imagesavealpha($rotate, TRUE);
Source: Comment in PHP imagerotate
Related
This is original image:
This is the code I am using:
<?php
$filename = 'image.jpg';
$degrees = 135;
// Content type
header('Content-type: image/jpeg');
// Load
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 1);
// Output
imagejpeg($rotate);
// Free the memory
imagedestroy($source);
imagedestroy($rotate);
?>
This is output:
In this picture background is black. I want to make it white after rotation. Could you help me?
please you can change your code just like that
$source = imagecreatefromjpeg($filename);
// Rotate
$transColor = imagecolorallocatealpha($source, 255, 255, 255, 127);
$rotate = imagerotate($source, $degrees,$transColor);
and try this i hope it will help you in color you can change it with any another color code
I use the below code to resize images (jpg, png, gif). The code is working perfectly. But the problem is after resizing the images, all transparent images (both png and gif) have a black background.
How can I maintain the transparency so that resized images will have not have black background?
$target = 'uploads/'.$newname;
move_uploaded_file( $_FILES['file']['tmp_name'], $target);;
$filename=$newname;
if($ext=='jpg'||$ext=='jpeg') {
$im = imagecreatefromjpeg('uploads/'.$filename);
} else if ($ext=='gif') {
$im = imagecreatefromgif('uploads/'.$filename);
} else if ($ext=='png') {
$im = imagecreatefrompng('uploads/'.$filename);
}
$ox = imagesx($im);
$oy = imagesy($im);
$nm = imagecreatetruecolor(400, 300);
imagecopyresized($nm, $im, 0,0,0,0,400,300,$ox,$oy);
imagejpeg($nm, 'thumbnails/' . $filename);
imagesavealpha() sets the flag to attempt to save full alpha channel
information (as opposed to single-color transparency) when saving PNG
images.
You have to unset alphablending (imagealphablending($im, false)), to
use it.
Try adding
imagealphablending( $nm, FALSE );
imagesavealpha( $nm, TRUE );
Here:
.
.
$nm = imagecreatetruecolor(400, 300);
imagealphablending( $nm, FALSE );
imagesavealpha( $nm, TRUE );
.
.
Also consider using imagecopyresampled instead of imagecopyresized.
imagecopyresampled() smoothly interpolates pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity.
Use imagecopyresampled($nm, $im, 0,0,0,0,400,300,$ox,$oy);
Instead of
imagecopyresized($nm, $im, 0,0,0,0,400,300,$ox,$oy);
I also had similar troubles where a black background still appeared when using:
imagealphablending($image, false);
imagesavealpha($image, true);
I found the below combination to be successful though:
imagecolortransparent($image, imagecolorallocate($thumbnail, 0, 0, 0));
imagealphablending($image, false);
I am taking a PNG image from a url as below.
I want to convert the PNG image to JPEG without saving disk with PHP.
Finally I want to assign JPEG image to $content_jpg variable.
$url = 'http://www.example.com/image.png';
$content_png = file_get_contents($url);
$content_jpg=;
Simplified answer is,
// PNG image url
$url = 'http://www.example.com/image.png';
// Create image from web image url
$image = imagecreatefrompng($url);
// Start output buffer
ob_start();
// Convert image
imagejpeg($image, NULL,100);
imagedestroy($image);
// Assign JPEG image content from output buffer
$content_jpg = ob_get_clean();
You want to use the gd library for this. Here's an example which will take a png image and output a jpeg one. If the image is transparent, the transparency will be rendered as white instead.
<?php
$file = "myimage.png";
$image = imagecreatefrompng($file);
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, TRUE);
imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagedestroy($image);
header('Content-Type: image/jpeg');
$quality = 50;
imagejpeg($bg);
imagedestroy($bg);
?>
How can I get the width and height of the image after rotating it using imagerotate() in PHP?
Here is my code:
<?php
// File and rotation
$filename = 'test.jpg';
$degrees = 180;
// Content type
header('Content-type: image/jpeg');
// Load
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
// Output
imagejpeg($rotate);
// Free the memory
imagedestroy($source);
imagedestroy($rotate);
?>
But what I want to do before doing the output, is that I want to get the width and height of the rotated image. How can I do that?
I'm sure you can do something similar to this:
$data = getimagesize($filename);
$width = $data[0];
$height = $data[1];
Another option is this:
list($width, $height) = getimagesize($filename);
imagerotate returns an image ressource. Hence you cannot use getimagesize which works with an image file.
Use
$width = imagesx($rotate);
$height = imagesy($rotate);
instead.
Hey guys i have a problem with merging two pictures...
I am trying to merge an png file (called badge) with an useruploaded picture.
Everything works fine when the user upload a png oder gif file, but if he uploads a jpeg image the output image looks really weird. It seems it is an color problem.
Here my code:
//Calculate position for badge (right bottom corner)
$badgeRightPosition = $imageWidth - $badgeWidth;
$badgeLeftPosition = $imageHeight - $badgeHeight;
$image = imagecreatefromstring(file_get_contents($image));
$badge = imagecreatefromstring(file_get_contents($badge));
$trueColorImage = imagecreatetruecolor($imageWidth, $imageHeight);
imagealphablending($trueColorImage, true);
imagesavealpha($trueColorImage, true);
imagealphablending($badge, true);
imagesavealpha($badge, true);
imagealphablending($image, true);
imagesavealpha($image, true);
imagecopyresized($trueColorImage, $image, 0, 0, 0, 0, $imageWidth, $imageHeight, $imageWidth, $imageHeight);
imagecopyresized($trueColorImage, $badge, $badgeRightPosition, $badgeLeftPosition, 0, 0, $imageWidth, $imageHeight, $badgeWidth, $badgeHeight);
Instead of imagecreatefromstring(file_get_contents($image)); I would try to directly open the file
$imgsrc = #ImageCreateFromJPEG($image);
if (!$imgsrc) $imgsrc = #ImageCreateFromPNG($image);
if (!$imgsrc) $imgsrc = #ImageCreateFromGIF($image);
if (!$imgsrc) $imgsrc = #ImageCreateFromWBMP($image);
Or check the file type by its ending and then use one of the functions above according to the file ending.