Image rotation and white background in php - php

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

Related

php Rotating image and resaving

So I have an image uploaded what I'm trying to do is have the user rotate it, receive that value and rotate the image and replace the original with the new image.
So I have a button:
Rotate
In my js file:
$('.rotate').on('click', function(e){
e.preventDefault();
if(rotate < 360)
rotate = rotate +90;
else
rotate = 0;
var id = $(this).data('id');
$('.img-container img').attr('style', '-webkit-transform: rotate(' + rotate + 'deg)');
$.ajax({
type: "POST",
url: "functions/post",
data: { rotate: rotate, id: id }
});
});
and inside "functions/post":
$degrees = $_POST['rotate'];
$postid = $_POST['postid'];
//user the postid to get the imagefilepath from db
$filename = '../img/uploads/' . $post->getimagepath();
// Content type
header('Content-type: image/jpeg');
// Load
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
// Output
imagejpeg($rotate);
list($width, $height) = getimagesize($rotate);
$newImage = imagecreatetruecolor($width, $height);
imagecopyresampled($newImage, $source, 0, 0, 0, 0, $width, $height, imagesx($source), imagesy($source));
imagejpeg($newImage, realpath('../img/uploads/' . $post->getimagepath()));
// Free the memory
imagedestroy($source);
imagedestroy($rotate);
imagedestroy($newImage);
but it doesn't save the rotated file.. :(
this code i got it from searching through so and google but can't seem to make it work for me yet. None of this code is set in stone so anything that needs changing aslong as it makes it work then its all good and greatly appreciated.
You're not saving the rotated image, you're saving the original image over again.
Try this:
$degrees = $_POST['rotate'];
$postid = $_POST['postid'];
//user the postid to get the imagefilepath from db
$filename = '../img/uploads/' . $post->getimagepath();
// Content type
header('Content-type: image/jpeg');
// Load
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
imagejpeg($rotate, realpath('../img/uploads/' . $post->getimagepath()));
// Free the memory
imagedestroy($source);
imagedestroy($rotate);
(not tested) but you get the idea.

php imagecopyresampled adds black background

I have a resize image script that takes a 130x81 image and adds it to a 130x130 image, when the imagecopyresampled function runs it adds a black background into the space that is left over, even though the base image is white. Code below, I could really appreciate some help.
The image I am trying to merge onto the 130x130 file php created is:
$width = 130;
$height = 130;
$filename = 'process-add.jpg'; //130x81px jpg
$this->_image = imagecreatefromjpeg($filename);
$background = imagecreatetruecolor(130,130);//create the background 130x130
$whiteBackground = imagecolorallocate($background, 255, 255, 255);
imagefill($background,0,0,$whiteBackground); // fill the background with white
imagecopyresampled($background, $this->_image,(130-$width)/2,(130-$height)/2, 0, 0, $width, $height, $width, $height); // copy the image to the background
ImageJpeg ($background,null,100); //display
I have read on multiple posts to add:
imagealphablending($background, false);
into the code which should fix it, but it doesn't make any difference.
Thanks in advance!
This has been solved. The issue was with teh width and height on the imagecopyresampled call. See the code block below:
<?
ini_set('allow_url_fopen', true);
$filename = 'http://img.yessy.com/1402152287-17201a.jpg'; // 130x81
$image = imagecreatefromjpeg($filename);
list($originalWidth, $originalHeight) = getimagesize($filename);
// Size of image to create
$width = 130;
$height = 130;
$background = imagecreatetruecolor($width, $height);//create the background 130x130
$whiteBackground = imagecolorallocate($background, 255, 255, 255);
imagefill($background,0,0,$whiteBackground); // fill the background with white
imagecopyresampled($background, $image, 0, ($height - $originalHeight) / 2, 0, 0, $originalWidth, $originalHeight, $originalWidth, $originalHeight); // copy the image to the background
header("Content-type: image/jpeg");
ImageJpeg ($background,null,100); //display
?>

Black background when rotating image with PHP

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

Black background when turning a PNG into a GIF

I have a PNG image I am trying to open and then output as a GIF image. However, the transparency is lost when I do this and the background turns black. If I output the image as a PNG, it works, but I specifically need to open the image as a PNG and the output it as a GIF.
This is what I have so far:
<?php
header("Content-type: image/gif");
$new_img = imagecreatefrompng($image);
imagealphablending($new_img, false);
imagesavealpha($new_img, true);
imagegif($new_img);
?>
However, imagepng($new_img) saves the background transparency but does not output as a GIF.
Try with this code:
<?php
header("Content-type: image/gif");
$new_img = imagecreatefrompng($image);
$trans_color = imagecolortransparent($new_img);
$trans_index = imagecolorallocate($new_img, $trans_color['red'], $trans_color['green'], $trans_color['blue']);
imagecolortransparent($new_img, $trans_index);
imagegif($new_img);
?>
So I managed to get this to work. Please let me know if anyone has a better solution:
<?php
header("Content-type: image/gif");
$new_img = imagecreatefrompng($image);
$background = imagecreatefrompng("background.png");
imagecopyresampled($background, $new_img, 0, 12, 0, 0, 100, 125, 100, 125);
$c = imagecolorat($background, 0, 0);
imagefilledrectangle($background, 0, 112, 100, 125, $c);
imagecolortransparent($background, $c);
imagegif($new_img);
?>

Applying text to png transparency issue PHP

I am trying to write text onto a png, however when I do it puts a dark border around it, I am not sure why.
The original image:
The processed image:
Code:
// Load the image
$im = imagecreatefrompng("admin/public/images/map/order/wally.png");
// If there's an error, gtfo
if(!$im) {
die("");
}
$textColor = imagecolorallocate($im, 68, 68, 68);
$width = imagesx($im);
$height = imagesy($im);
$fontSize = 5;
$text = "AC";
// Calculate the left position of the text
$leftTextPos = ($width - imagefontwidth($fontSize)*strlen($text)) / 2;
// Write the string
imagestring($im, $fontSize, $leftTextPos, $height-28, $text, $textColor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
I've had this issue several times, let me find the answer...
Ok, found something:
imagesavealpha($im, true);
imagealphablending($im, true);
Write that before imagepng.
Yes, saving with alpha is important but loading it is important as well. Your PNG image might have transparency but it is good practice to account for that as well.
You'd need to create true color image, set alpha color and then draw your loaded image with text over it. So something like this:
// create true color image
$img = imagecreatetruecolor($width, $height);
$transparent_color = imagecolorallocatealpha($img, 255, 255, 255, 0);
imagealphablending($img, false);
imagefillrectangle($img, 0, 0, $width, $height, $transparent_color);
imagealphablending($img, true);
// draw previously loaded PNG image
imagecopy($img, $loaded_img, 0, 0, 0, 0, $width, $height);
// draw your text
// save the whole thing
imagesavealpha($img, true);
imagepng($img, $file);

Categories