WideImage - Canvas with transparent background - php

We're using a CMS with WideImage built in and no scope to change this, however we've come up against a situation where we need to produce 300x300 images.
However the images that are uploaded at approx 100x100 in jpg and png format with various levels of transparency.
I'm trying to create a 300x300 canvas, and place the 100x100 image inside of it - however I want both the canvas to remain transparent, and the image placed on top to keep its transparency.
I've got
$image = WideImage::loadFromFile( $this->local_path );
$canvas = WideImage::createTrueColorImage(300, 300);
$canvas_bg = $canvas->allocateColor(255, 255, 255);
$canvas->fill(0, 0, $canvas_bg);
$resized_image = $canvas->merge($image);
However this obviously just adds a white background to the canvas, I cant figure out from their documentation how to make it transparent.
Thanks

How about:
$image = WideImage::loadFromFile($this->local_path);
$resized_image = $image->resizeCanvas(300, 300, 0, 0);
You can specify the X and Y location of the image within the new canvas as well as its width and height. $image->resizeCanvas(300, 300, "center", "center") works quite well.

Related

Thumbnail geration in php is creating unwanted black colour

I have this image cropping application in jquery + php. Jquery sends the co-ordinates to php and php preety much makes thumbnail out of it. The code is below.
$source_image = imagecreatefromjpeg('../../../uploads/'.$dir_name.'/'.$image_name);
$virtual_image = imagecreatetruecolor($width_img, $height_img);
imagecopyresampled($virtual_image, $source_image, 0, 0, $x_img, $y_img, 225, 225, $width_img, $height_img);
imagejpeg($virtual_image, $dest);
Everything is correct in the jquery part. It sends all the co-ordinates correctly. Now the problem arises when I crop the image greater than 225 px in width and 225 pixels in height. Unwanted black colour appears as in the image. The image should have been 225*225.. Only the image part, not the black part.
Example:
ive been using this code and it works fine for my thumbnails
$resource = imagecreatefromjpeg($filepath);
$thumb = imagetruecolor($thumbwidth,$thumbheight);
imagecopyresampled($thumb,$resources,0,0,0,0,$thumbwidth,$thumbheight,$originalWidth,$originalHeight);

Creating a transparent canvas with GD

I am trying to create a transparent image canvas and then place other random images on top of this canvas. I then finally save the final image as a gif. I have tried the below:
$canvas = imagecreatetruecolor($this->canvas_width, $this->canvas_height);
imagesavealpha($canvas, true);
imagealphablending($canvas, false);
$trans_colour = imagecolorallocatealpha($canvas, 0, 0, 0, 127);
imagefill($canvas, 0, 0, $trans_colour);
However, if there is some unused space left on the canvas then this part is black. I thought this area should be transparent?
Am I applying transparency correctly with the above?
imagesavealpha can not be used for images you eventually intend to save as GIF because GIF images do not have alpha channels. From the docs:
imagesavealpha — Set the flag to save full alpha channel
information (as opposed to single-color transparency) when saving PNG
images
So it only works if you are going to save the image as PNG. GIF images can do transparency, but not with an alpha channel; they can only be "transparent" or "not transparent" by defining a particular color as transparent. In PHP you can do that using imagecolortransparent. For example, to make all black transparent, you could do:
$black_color = imagecolorallocate($canvas, 0, 0, 0);
imagecolortransparent($canvas, $black_color);

Transparent PNG over JPG in PHP

What seems to be simple, isn't :(
I'm trying to add something like a watermark (transparent png) on an image (jpg).
This is the code I'm using:
$width = 800;
$height = 600;
$bottom_image = imagecreatefromjpeg("portrait1.jpg");
$top_image = imagecreatefrompng("man2.png");
imagesavealpha($top_image, true);
imagealphablending($top_image, true);
imagecopy($bottom_image, $top_image, 200, 200, 0, 0, $width, $height);
header('Content-type: image/png');
imagepng($bottom_image);
When I merge the images, the png is positioned at the right place, everythig above and left of it is good (jpg is copied), but everything else is black.
I've tried setting imagesavealpha and imagealphablending to false, there wasn't any difference.
You can see the resulting image at http://ekstrakt.selfip.com/photobomb/image.php
I've searched around the net, I can't find a solution.
Any help is appreciated.
Your $width and $height should be the dimensions of the watermark, not of the photo. What you're telling it to do is copy the watermark with a much bigger size than it is. When it reads part of an image that doesn't exist (coordinates out of bounds) the result is opaque black, giving the result you see.
Use imagecopymerge() instead of imagecopy()
U may also like imagesavealpha()

dynamic image resize using php

I have an image which i am going to be using as a background image and will be pulling some other images from the database that i want to show within this image. So if i am pulling only 1 image, i want the bottom part of the background image to close after the first image, if there are multiple images then i want it close after those images are shown. The problem with not using separate images is that the borders of the images have a design format and i cannot show them separately.
Take a look at this image . The design format of the right and left borders are more complicated than that to just crop them and use them. Any suggestions if there is any dynamic image resizing thing?
Yes there is. Look at the imageXXXX functions; the ones you are particularly interested in are imagecreate, imagecreatetruecolor, imagecreatefrompng, imagecopyresampled, imagecopyresized, and imagepng (assuming you're dealing with PNG images - there's similar load / save functions for jpeg, gif, and a few other formats).
You should try using the GD extension for PHP, especially have a look at imagecopyresized(). This allows you to do some basic image conversion and manipulation very easily.
A basic example that takes two GET parameters, resizes our myImage.jpg image and outputs it as a PNG image:
<?php
// width and height
$w = $_GET['w'];
$h = $_GET['h'];
// load image
$image = imagecreatefromjpeg('myImage.jpg');
// create a new image resource for storing the resized image
$resized = imagecreatetruecolor($w, $h);
// copy the image
imagecopyresized($resized, $image, 0, 0, 0, 0, $w, $h, imagesx($image), imagesy($image));
// output the image as PNG
header('Content-type: image/png');
imagepng($resized);
Have you tried PHPThumb? I used this class often and its pretty clean and lightweight. I used it here.

PHP/GD ImageSaveAlpha and ImageAlphaBlending

I'm using GD to resize and convert images, however during my tests I found a weird behavior when converting transparent PNG's to JPEG's. According to the manual ImageAlphaBlending() is on by default but in order to preserve the transparency I must set ImageSaveAlpha() to true (which in turn requires that I set ImageAlphaBlending() to false). So the correct way should be:
$result = ImageCreateFromPNG(...);
ImageAlphaBlending($result, false);
ImageSaveAlpha($result, true);
ImageFill($result, 0, 0, IMG_COLOR_TRANSPARENT);
ImageJPEG($result);
ImageDestroy($result);
However if I do it the "correct" way all the transparency area comes up black in the JPEG. This seems to work (JPEG with white background on transparent areas) on my tests:
$result = ImageCreateFromPNG(...);
ImageAlphaBlending($result, true); // true by default, but still...
ImageSaveAlpha($result, true);
ImageFill($result, 0, 0, IMG_COLOR_TRANSPARENT);
ImageJPEG($result);
ImageDestroy($result);
Can someone please enlighten me on this subject?
It probably depends on your PNG. A PNG file can contain a background color, which can be used when transparency doesn't work. Your PNG probably has a white background. When you set imageaplhablending to true it picks up the background color from your PNG and uses that when writing the JPEG. When you set it to false it picks the default for GD which is black.
You can try it for yourself. Create a transparent PNG and save it with an orange or pink background color. Your second example should show that color.
By the way, the PNG background color trick is a nice one for IE6 images. IE6 does not support transparent PNGs so it will display them with whatever background color you saved them with. When saving transparent PNGs, save them with the same background color as your website. It will look better than white or black boxes around your PNG images in IE6.
If you are converting from PNG (or GIF) to JPG, you should probably copy the final image to another image that is filled with white, using imagecopy ($image is any image already created with GD):
// Create a new background
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
// Allocate the color
$color = imagecolorallocate($bg, 255, 255, 255);
// Fill the background with white
imagefill($bg, 0, 0, $color);
// Alpha blending must be enabled on the background!
imagealphablending($bg, TRUE);
// Copy the current image onto the opaque background
if (imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image)))
{
// Replace the image with the background copy
imagedestroy($image);
$image = $bg;
}
Hope that helps.

Categories