increasing image size with GD and PHP - php

i seem to be too dumb for this :-)
i have an image say
x=200
y=200
i want the image to stay the same but include a footer watermark lets say a.jpg which has a height of 20
so i want
a final image of 220 and y 200
i tried it this way but it wont work
$newimage=imagecreatetruecolor($width,$height+25);
imagecopy($newimage, $this->parentInstance->getOldImage(), 0, 0, 0, 0, $watermarksize[0], $watermarksize[1]);
imagecopy($newimage, $watermark, $dest_x, $dest_y, 0, 0, $watermarksize[0], $watermarksize[1]);
imagecopy($this->parentInstance->getOldImage(), $newimage, 0, 0, 0, 0, $dest_x, $dest_y);

You need to lookup imagecopymerge() - http://php.net/manual/en/function.imagecopymerge.php

Look at image copy resized if you want to resize an image resource using the GD library.

Related

"z-index" in php GD

In css, we have a property called "z-index", what is the same in PHP GD and Image Functions to control the "z-index"?
I've been searching but I can't find one, please help.
Thankyou
there nothing existing like z-index in php GD library
but there several ways to overlap image over image or text over image
$redimg = imagecreatetruecolor(100, 100);
$image = imagecreatefrompng('image.png');
// sets background to red
$red = imagecolorallocate($redimg, 255, 0, 0);
imagefill($redimg, 0, 0, $red);
// Merge the red image onto the PNG image
imagecopymerge($image, $redimg, 0, 0, 0, 0, 100, 100, 75);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
imagedestroy($redimg);
here one example, or let me know, what exactly you trying to do, I will help you
There's more information here.

PHP, imagecreate, imagecreatetruecolor, imagecopy, imagecopymerge

(1) imagecreate
(2) imagecreatetruecolor
(3) imagecopy
(4) imagecopymerge
I use above PHP function and get confused.
First I prepare two png files...
1.png http://www.capbite.com/subfolder/lion/test/1.png
A QRcode with transparent background and black dot, I create it by these code (only keypoint, not all).
$image = imagecreate($width, $height);
imagesavealpha($image, true);
$color1 = imagecolorallocatealpha($image, 0, 0, 0, 127);
imageColorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $color1);
...
imagepng($image, $filename);
2.png http://www.capbite.com/subfolder/lion/test/2.png
I make a png and use a color to fill its background by MS paint.
Prepare done. Then the code is
<?php
//use imagecreate or imagecreatetruecolor
$image = imagecreate(50, 50);
//$image = imagecreatetruecolor(50, 50);
//save the alpha channel
imagesavealpha($image, true);
//alphablending set false, then transparent color can cover the canvas
imagealphablending($image, false);
//take a transparent color and fill it
imagefill($image, 0, 0, imagecolorallocatealpha($image, 0, 0, 0, 127));
//draw the ellipse
imagefilledellipse($image, 15, 15, 30, 30, imagecolorallocate($image, 255, 0, 0));
imagepng($image, '3.png');
imagedestroy($image);
//merge image
$im_background = imagecreatefrompng('1.png');
$im_foreground = imagecreatefrompng('3.png');
list($width, $height) = getimagesize('3.png');
//use imagecopy or imagecopymerge
imagecopy($im_background, $im_foreground, (int)35, (int)35, 0, 0, $width, $height);
//imagecopymerge($im_background, $im_foreground, (int)35, (int)35, 0, 0, $width, $height, 100);
imagepng($im_background, 'x-x.png');
imagedestroy($im_background);
imagedestroy($im_foreground);
The code make 3.png like this www.capbite.com/subfolder/lion/test/3.png
Now if I use
1.png + imagecreate + imagecopy will get http://www.capbite.com/subfolder/lion/test/4-1.png
1.png + imagecreate + imagecopymerge will get http://www.capbite.com/subfolder/lion/test/4-2.png
1.png + imagecreatetruecolor + imagecopy will get http://www.capbite.com/subfolder/lion/test/4-3.png
1.png + imagecreatetruecolor + imagecopymerge will get http://www.capbite.com/subfolder/lion/test/4-4.png
2.png + imagecreate + imagecopy will get http://www.capbite.com/subfolder/lion/test/5-1.png
2.png + imagecreate + imagecopymerge will get http://www.capbite.com/subfolder/lion/test/5-2.png
2.png + imagecreatetruecolor + imagecopy will get http://www.capbite.com/subfolder/lion/test/5-3.png
2.png + imagecreatetruecolor + imagecopymerge will get http://www.capbite.com/subfolder/lion/test/5-4.png
It is hard to describe the image that made by different ways, so I paste the link about the images, but my reputation is not enough to post more links or images... -- Edited by Niet ;)
My question is, just the 3.png, it has a transparent background, even use different create function, but when it copy to another image which I created or by MS paint, the result is I meet.
What's different between imagecreate & imagecreatetruecolor make 4-1.png and 4-3.png ?
What's different between imagecopy & imagecopymerge(even pct set 100) make 4-3.png and 4-4.png ?
What's different between the images which I created or by MS paint make 4-3.png and 5-3.png ?
Thanks a lot!
I would recommend using imagecreate, not imagecreatetruecolor.
When using imagecreate, the first colour you define will be the background colour that fills the entire canvas. This will usually be your "transparent" colour, however it is generally better (read: more efficient in the file) to do it this way:
imagecolortransparent($img, imagecolorallocate($img, 255, 0, 255));
In this way, you aren't using an alpha channel, you're just telling it "anything I draw in bright pink will be transparent" - this is how transparency works in GIF images, by the way, and it's also the "traditional" way to do it, way back on old games from many many years ago!
Using imagecreate-generated images, copying works a lot easier, because GD knows exactly what colour is "transparent" and therefore shouldn't be copied on to the target. When using imagecreatetruecolor, then you get into the complicated and messy business of compositing...
I hope this helps you out. GD can be a tricky beast to master, but once you know the basics you should be good to go.

Create a tiled image with offset

In the process of creating a tiled image I'd like to set an offset (so that the tile doesn't start at 0, 0) but when I provide what I'd expect to give me the correct image, it's not rendered correctly.
I'm setting the tile using imagesettile($image, $tile); but when I go to draw it (using imagefilledrectangle($image, 10, 10, 300, 300, IMG_COLOR_TILED);), I get an image as though it was tiled from 0, 0 (with the top & left 10 pixels black) instead of it being tiled from 10, 10.
Any ideas as to how I can get it tiled from 10, 10 or do I have to create another tiled image and copy it across?
In the end I had to create a new image which was tiled and copy it in. The final code is along the lines of:
$transparent = imagecolorallocatealpha($background_image, 255, 255, 255, 127);
$tiled_image = imagecreatetruecolor($width, $height);
imagefill($tiled_image, 0, 0, $transparent);
imagesettile($tiled_image, $tile);
imagefilledrectangle($tiled_image, 0, 0, $width, $height, IMG_COLOR_TILED);
imagecopyresampled($image, $tiled_image, $sx, $sy, 0, 0, $ex, $ey, $width, $height);

Image transparency in PHP GD library

I need to create a PNG image from 4 parts of another PNG image with different levels of transparency using GD library in PHP. For example:
Result should look like this
I tried to do this thing in different ways but I coudn't achieve the desired result.
Thank you in advance ;)
Load your image with imagecreatefrompng(). Create a truecolor image with imagecreatetruecolor() and then set it fully transparent with imagecolorallocatealpha() and imagefill(). Then set alpha blending mode for both the source and destination images with imagealphablending(). After this you can use imagecopymerge() to copy the image with alpha.
Unfortunately it's not possible to enforce an alpha multiplier for imagecopymerge(), so this will get you only halfway there -- Inconvenient options include repeating imagecopymerge() calls on those parts of the image you want to be less transparent, choosing between several source images depending on the level of transparency you want to use, or going through the image pixel-by-pixel, which is inconveniently slow.
If you are not married to the image* functions, consider using ImageMagick instead. It is much more robust.
I did this:
$output = imagecreatetruecolor([width], [height]);
imagesavealpha($output , true);
$trans_colour = imagecolorallocatealpha($output , 0, 0, 0, 127);
imagefill($output , 0, 0, $trans_colour);
Now the image is transparent :)
The whole script:
$output = imagecreatetruecolor([width], [height]);
imagesavealpha($output , true);
$trans_colour = imagecolorallocatealpha($output , 0, 0, 0, 127);
imagefill($output , 0, 0, $trans_colour);
header('Content-Type: image/png');
imagepng($output);
imagedestroy($output);
Hope it helps!
This worked for me with both gif and png (of course, change every reference of png in this example to gif if using that image type).
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
$colour = imagecolorallocate($virtual_image,255,255,255);
imagefill($virtual_image , 0, 0, $colour);
imagealphablending($virtual_image,true);
imagesavealpha($virtual_image , true);
//the next line only if you're resizing to a new $width/$height, otherwise leave this line out
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
header('Content-Type: image/png');
if (imagepng($virtual_image)) imagedestroy($virtual_image);

Merging Images using GD with PHP

i'm working on creating one PNG image from two others.
Image A and B have the same dimensions, they are both 200x400px. The final image the same.
I'm using the GD library with PHP.
So my idea was to create a PNG-24 from my original PNG-8, then use color transparency and finally copy the second image into
this new PNG-24. The problem appears in the first step anyway, when going from PNG-24 to PNG-8 with color transparency:
This is to get the original PNG-8 and it's dimensions:
$png8 = imagecreatefrompng($imageUrl);
$size = getimagesize($imageUrl);
Now i create a new PNG and fill it's background with a green color (not present in the images):
$png24 = imagecreatetruecolor($size[0], $size[1]);
$transparentIndex = imagecolorallocate($png24, 0x66, 0xff, 0x66);
imagefill($png24, 0, 0, $transparentIndex);
This is for making the green color transparent:
imagecolortransparent($png24, $transparentIndex);
Then i copy the png8 into the PNG-24:
imagecopy($png24, $png8, 0, 0, 0, 0, $size[0], $size[1]);
So here's the problem: the original PNG-8 looks good, but it has a green border surrounding the shape within the original image. It's difficult to explain really. Seems like some part of the green background is left in the remaining PNG.
What can i do?
thanks in advance
best regards,
Fernando
I had some problems with png transparency before and was able to solve them with this pattern:
// allocate original image to copy stuff to
$img = imagecreatetruecolor(200, 100);
// create second image
$bg = imagecreatefrompng('bg.png');
// copy image onto it using imagecopyresampled
imagecopyresampled($img, $bg, 0, 0, 0, 0, 200, 100, 200, 100);
imagedestroy($bg);
// create third image
// do same routine
$fg = imagecreatefrompng('fg.png');
imagecopyresampled($img, $fg, 50, 50, 0, 0, 50, 50, 50, 50);
imagedestroy($fg);
// output image
imagepng($img);
imagedestroy($img);
I think the only difference between mine and yours is imagecopy() vs. imagecopyresampled(). I seem to remember having problems with that though it was quite a while ago. You can see an example of an image I use this pattern on here: http://www.ipnow.org/images/1/bggrad/bg4/yes/TRANSIST.TTF/8B0000/custombrowserimage.jpg (I allocate a blank image, copy the background image in, copy the overlay with transparency in)

Categories