Transparent PNG Going Black - php

I have looked through so many other threads with people having trouble with a png image going black where it should be transparent and none of the solutions have worked for me.
Maybe I am going wrong else where in the code? Maybe imagealhpablending isnt supported by my web server?
Thanks for anyone that can help.
$photo = imagecreatefrompng( "{$thumb_folder}{$new_file_name}" );
$width = imagesx($photo);
$height = imagesy($photo);
$new_width = 32;
$new_height = floor($height / ($width / $new_width));
$temp_photo = imagecreatetruecolor($new_width, $new_height);
imagealphablending($temp_photo, false);
imagesavealpha($temp_photo, true);
$transparent = imagecolorallocatealpha($temp_photo, 255, 255, 255, 127);
imagefilledrectangle($temp_photo, 0, 0, $new_width, $new_height, $transparent);
imagecopyresampled($temp_photo, $photo, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagepng($temp_photo, "{$thumb_folder}{$new_file_name}" );

The first colour allocated to an image is always the background colour anyway, so there is no need to draw that filled rectangle. Remove it from the code, and see what you get.

Related

PHP: Adding opacity with imagefilter() blackens already transparent areas

I'm currently writing a script to add watermarks to images. Said watermarks vary in their opacity.
For example, the base watermark image is a PNG with a fully visible text and a transparent background. When added, I want to fade this base PNG to fit my needs and to make an opaque watermark.
To do that, I use imagefilter() to fade the PNG:
$opacity = 0.25;
$watermarkRes = imagecreatefrompng($filename);
imagealphablending($watermarkRes, false);
imagesavealpha($watermarkRes, true);
$transparency = 1 - $opacity;
imagefilter(
$watermarkRes,
IMG_FILTER_COLORIZE,
0,
0,
0,
127*$transparency
);
imagepng($watermarkRes, $filename);
All non-transparent areas get faded nicely, but the existing transarent areas get blackened.
This is the result of the above code:
https://preview.ibb.co/j8zePF/TEST.png
Used as a watermark it looks like this:
https://preview.ibb.co/mLvKPF/15027295625991d55a1ef081_42502547.jpg
While the desired result should be this:
https://preview.ibb.co/f81R4F/TEST_15027295625991d55a1ef081_42502547.jpg
How can I achieve to add opacity to the text while maintaining the transparent areas as they are?
Nevermind. The issue was not the function itself, but the fact that I resized the height of the watermark according to its use beforehand.
I removed this code to resize the watermark:
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $watermarkRes, 0, 0, 0, 0, $width, $height, imagesx($watermarkRes), imagesy($watermarkRes));
$watermarkRes = $new_image;
And instead used the resize code of Dycey provided in the answer to this question:
How do I resize pngs with transparency in PHP?
For my case I created this function to resize images:
/**
* #param int $width
* #param int $height
*/
public function resize($width, $height)
{
$new_image = imagecreatetruecolor($width, $height);
if($this->image_type === IMAGETYPE_PNG || $this->image_type === IMAGETYPE_GIF) {
imagealphablending($new_image, false);
imagesavealpha($new_image,true);
$transparent = imagecolorallocatealpha(
$new_image, 255, 255, 255, 127
);
imagefilledrectangle(
$new_image, 0, 0, $width, $height, $transparent
);
}
imagecopyresampled(
$new_image,
$this->image,
0, 0, 0, 0,
$width, $height,
imagesx($this->image), imagesy($this->image)
);
$this->image = $new_image;
}

PHP increase image resolution from url and then crop

I have images with resolution of 720x1280 and I would need to store them on server with resolution 800x1500.
Since 720x1280 increased to height 1500 gives resolution of 844x1500 I would also need to crop image, remove 22 pixels from the left and right side.
For now I have this:
$img_url = file_get_contents($url);
$img = imagecreatefromstring($img_url);
$width = imagesx($img);
$height = imagesy($img);
$new_width = '800';
$new_height = '1500';
$thumb = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($thumb, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($thumb, $name, 100);
imagedestroy($thumb);
imagedestroy($img);
But the problem is that image is not cropped, 22 pixels from the left and right side are not removed.
Is there a way to do this, to first increase image resolution from url and then crop?
Googling php image crop reveals the secret:
$rect = [22, 0, 800, 1500]
$thumb = imagecrop($thumb, $rect)
In this line:
imagecopyresampled($thumb, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
you just scale your original image to new dimensions.
As the documentation says:
If the source and destination coordinates and width and heights differ, appropriate stretching or shrinking of the image fragment will be performed.
You need to set:
$shiftX = 22*(1280/1500); // consider passing variables rather than constant values
$scaledWidth = 720-$shiftX*2;
imagecopyresampled($thumb, $img, 0, 0, $shiftX, 0, $new_width, $new_height, $scaledWidth, $height);
which cuts out the required (proportional) rectangle of the source image to paste it in you destination picture.

Trouble preserving transparency when resizing image that was a jpg

We get a jpg that contains a drawing made of black lines. We do crop it, rotate it, make the white transparent, then finally resize it to a standard width.
Before I resize it, the image (in $src) is just what I want it to be with transparency in the right places. After resampling it, the image ($out) is back to having a white background. (The commented out lines are some of the things I tried.) Before I found an answer to a similar problem, I wasn't changing the settings for alpha blending and alpha save and there was at least some very noisy transparency.
How can I get the resampled image to change the white to transparent?
EDIT: In $out I see that most pixels are 255, 255, 255. Some are 252, 252, 252. A few are 245, 245, 245. Those are the only 3 values I have seen in $out. I'm not understanding why this would be the case for $out but not for $src.
<?php
$imgname = "../assets/Sample.jpg";
$src = imagecreatefromjpeg($imgname);
$src = imagecropauto($src, IMG_CROP_WHITE);
$white = imagecolorallocate($src, 255, 255, 255);
imagecolortransparent($src, $white);
$src = imagerotate($src, -90, 0);
// Resample
$width = imagesx($src);
$height = imagesy($src);
$percent = 270/$width;
$new_width = $width * $percent;
$new_height = $height * $percent;
$out = imagecreatetruecolor($new_width, $new_height);
//imagefill($out, 0,0, imagecolorallocate($out, 255, 255, 255));
imagealphablending( $out, false );
imagesavealpha( $out, true );
imagecopyresampled($out, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$white2 = imagecolorallocate($out, 255, 255, 255);
imagecolortransparent($out, $white2);
header("Content-type: image/png");
// imagepng($src);
imagepng($out);
?>
The problem with drawing in JPEG is that the compression process slightly changes values. This is not generally noticeable in photos but shows up in drawings.
If you know everything is black or write, you should convert the pixels that are close-to-black to black and those that are close-to-white to white.

PHP Resize image and save Transparent PNG

I've a png image uploading script in my website and i'm scaling down the large images to small size and i'm using this code but it loses the transparent background
$image = $tmp; // Uploaded Image
$maxImgWidth = 224;
$src = imagecreatefrompng($image);
list($width, $height) = getimagesize($image);
$newwidth = $maxImgWidth;
$newheight = ($height / $width) * $newwidth;
$newImage = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($image, true);
imagesavealpha($image, true);
imagecopyresampled($newImage, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($newImage, "../thumb/$name_after-$code.$ext", 1);
imagedestroy($src);
imagedestroy($newImage);
This code makes the image without transparency :
And i want with transparent background like this one :
Sorry I have no experience in PHP but after I read this page, I think you may need add the transparent colour after you set the save alpha is true
imagesavealpha($image, true);
$color = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $color);
if still cannot try add this after imagecolorallocatealpha(),
imagecolortransparent($image, $color);

PHP - cropping image with imagecopyresampled()?

I'd like to crop an image using imagecreatetruecolor and it always crops it leaving black spaces, or the zoom is too big. I want the image to be exactly 191px wide and 90px high, so I also need to resize the image, as well as crop, because the ratio has to be kept. Well, there are some samples of the project:
The resize script (simplified) goes like this:
$src_img=imagecreatefromjpeg($photoTemp);
list($width,$height)=getimagesize($photoTemp);
$dst_img=imagecreatetruecolor(191, 90);
imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['crop']['width'], $newImage['crop']['height'], $width, $height);
The $newImage['crop'] array includes:
['x'] => $_POST['inp-x']
['y'] => $_POST['inp-x']
['width'] => $_POST['inp-width']
['height'] => $_POST['inp-height']
But what I get is:
Anyone sees, what I'm doing wrong?
Thanks, Mike.
you can do it too, I myself did it, and it works
(x1,y1)=> where crop starts
(x2,y2)=> where crop ends
$filename = $_GET['imageurl'];
$percent = 0.5;
list($width, $height) = getimagesize($filename);
$new_width = $_GET['x2'] - $_GET['x1'];
$new_height = $_GET['y2'] - $_GET['y1'];
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0 , $_GET['x1'] , $_GET['y1'] , $new_width, $new_height, $new_width, $new_height);
// Outputs the image
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
Try
<?php
$dst_img = imagecreatetruecolor($newImage['crop']['width'], $newImage['crop']['height']);
imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], 0, 0, $width, $height);
Ok, I found the problem myself, the code should be like this:
imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['newWidth'], 191, 90, $newImage['crop']['height']);
There's also the imagecrop function, which allows you to pass in an array with x, y, width, and height values.

Categories