I've got a problem making alpha PNGs with PHP GD. I don't have imageMagik etc.
Though the images load perfectly well in-browser and in GFX programs, I'm getting problems with Flash AS3 (actionscript) understanding the files. It complains of being an unknown type. But, exporting these files from Fireworks to the same spec works fine. So I'm suggesting it's something wrong with the formatting in PHP GD.
There seems to be a number of ways of doing this, with several similar functions; so maybe this isn't right?
$image_p = imagecreatetruecolor($width_orig, $height_orig);
$image = imagecreatefrompng($filename);
imagealphablending($image_p, false);
ImageSaveAlpha($image_p, true);
ImageFill($image_p, 0, 0, IMG_COLOR_TRANSPARENT);
imagealphablending($image_p, true);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width_orig, $height_orig, $width_orig, $height_orig);
imagepng($image_p, "new2/".$filename, 0);
imagedestroy($image_p);
This just takes files it's given and puts them into new files with a specified width/height - for this example it's same as original but in production it resizes, which is why I'm resampling.
To keep the transparency you should do
imagealphablending($image_p, false);
instead of "true". Maybe that will solve the format problem too.
Related
Hey guys i am trying to crop uploaded image using php using following code.
if(isset($_POST["Crop"])){
$source_x=$_POST['x_value'];
$source_y=$_POST['y_value'];
$width=$_POST['w_value'];
$height=$_POST['h_value'];
$src="assets/images/uploads/1509432972_Koala.jpg";
$crop = imagecreatetruecolor($width,$height);
imagecopy ($crop, $src, 0, 0, $source_x, $source_y, $width, $height);
imagejpeg($crop);
imagedestroy($src);
imagedestroy($crop);
}
The problem that it is not working properly. Since there are lot of posts there to solve this, found a solution called GD library how to include it and crop all kind of images.
I'm trying to make imageA.png transparent then merge it to another fully transparent image using imagecopy function, But after merging, imageA.png becomes white background instead of transparent
I believe it is because it was not converted to transparent properly
Here's what I used
This is a stand-alone file to convert the image to transparent ( It works ) :
$img = imagecreatefrompng("imageA.png");
$white = imagecolorallocate($img, 255, 255, 255);
imagecolortransparent($img, $white);
header('Content-Type: image/png');
imagepng($img);
and this one is to merge the above result with another transparent image
$src = imagecreatefrompng("imageA_transparent.png");
$dest = imagecreatefrompng('another_transparent_image.png');
imagesavealpha($dest, true);
imagealphablending($dest, true);
imagecopy($dest, $src, 0, 0, 0, 0, 400, 400);
the result is imageA_transparent.png becomes with white background while it is actually transparent over the second transparent used image (another_transparent_image.png)
I tried using a normal photoshopped transparent image and it worked with no problem
I tried to use both imagesavealpha and imagealphablending with $src and no effect
Both images are properly transparent before using imagecopy
** Finally, when i tried to open imageA_transparent.png ( The php-generated transparent image ), In photoshop, it appeared with white background and locked layer, This means PHP didn't convert it to transparent/png properly ( I believe )
I am trying to create the jpeg image using the imagejpeg but that outputs the image to browser and I don't want that.
$im = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($im, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
$im=imagejpeg($im, 'test.jpg', 100);
It comes up as if it doesn't have a jpg stored inside $im
I am trying to re-size an image and then work with it afterwards in the jpg format
In PHP, an image hasn't any type once it was created (from imageCreateFromJpeg(), imageCreateTrueColor(), …) : this is simply a ressource.
The conversion in a specific type takes place afterward, when you use imageJpeg() for instance. imageJpeg() outputs the image to browser or saves it in a file. That's all, it returns nothing more than a boolean which say if the conversion was a success or not.
I've been trying to get transparency to work with my application (which dynamically resizes images before storing them) and I think I've finally narrowed down the problem after much misdirection about imagealphablending and imagesavealpha. The source image is never loaded with proper transparency!
// With this line, the output image has no transparency (where it should be
// transparent, colors bleed out randomly or it's completely black, depending
// on the image)
$img = imagecreatefromstring($fileData);
// With this line, it works as expected.
$img = imagecreatefrompng($fileName);
// Blah blah blah, lots of image resize code into $img2 goes here; I finally
// tried just outputting $img instead.
header('Content-Type: image/png');
imagealphablending($img, FALSE);
imagesavealpha($img, TRUE);
imagepng($img);
imagedestroy($img);
It would be some serious architectural difficulty to load the image from a file; this code is being used with a JSON API that gets queried from an iPhone app, and it's easier in this case (and more consistent) to upload images as base64-encoded strings in the POST data. Do I absolutely need to somehow store the image as a file (just so that PHP can load it into memory again)? Is there maybe a way to create a Stream from $fileData that can be passed to imagecreatefrompng?
you can use this code :
$new = imagecreatetruecolor($width, $height);
// preserve transparency
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
imagealphablending($new, false);
imagesavealpha($new, true);
imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);
imagepng($new);
imagedestroy($new);
It will make a transparent image for you. Good Luck !
Blech, this turned out to ultimately be due to a totally separate GD call which was validating the image uploads. I forgot to add imagealphablending and imagesavealpha to THAT code, and it was creating a new image that then got passed to the resizing code. Which should probably be changed anyway. Thanks very much to goldenparrot for the excellent method of converting a string into a filename.
Do I absolutely need to somehow store the image as a file (just so that PHP can load it into memory again)?
No.
Documentation says:
You can use data:// protocol from php v5.2.0
Example:
// prints "I love PHP"
echo file_get_contents('data://text/plain;base64,SSBsb3ZlIFBIUAo=');
I am currently trying to get a form which will allow multiple images to be uploaded and resized on the server using PHP. Each uploaded image by the client is around 2.5mb in size.
I am currently using the move_uploaded_file() function.
There are no issues to move the files onto the server. The problem arises when I try to crop. Having not ImageMagick on my host I am using this setup (not all the code just what's relevant, this is in a loop with $width etc. changing for the different crop sizes)
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, $output_filename, 80);
As it stands this will only work for 2 images. If there is 3 or more submitted I get a 'memory exhausted' error. I have researched into this as my memory limit is 120mb. Apparently the imagecreatefromjpeg function uses a lot of memory, especially if the file has a large resolution (which mine does - hence why I need them cropped/resized).
Does anyone know of a more efficient approach to this task? I have researched on google but everyone uses the same technique I am.
Use imagedestroy to clear any memory associated with $image and $image_p :
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, $output_filename, 80);
imagedestroy($image);
imagedestroy($image_p);