So I have a 3x3 pixel image using imagecreate. I want to scale up the image with imagescale while maintaining the look of a 3x3 grid of "pixels". However, the pixels on the right and bottom edge are not the same size.
Here is my code and output image:
<?php
$image = imagecreate(3, 3);
imagecolorallocate($image, 0, 0, 255);
$red = imagecolorallocate($image, 255, 0, 0);
imagesetpixel($image, 0, 0, $red);
imagesetpixel($image, 1, 1, $red);
imagesetpixel($image, 2, 2, $red);
imagepng(imagescale($image, 200, 200, IMG_NEAREST_NEIGHBOUR));
header("Content-Type: image/png");
This is my output:
Notice how the bottom-right pixel is cut off. I kept playing with the numbers for the new dimensions and arrived at 256x256 at which point the pixels are all the same size.
This is the output after using 256x256:
My question is: How can I derive the dimensions to use for the resized image with the effect I described?
Bonus question: Is an alternative method which will allow me to resize to an arbitrary size and keep the pixels approximately the same size?
I would use imagecopyresampled to achieve this.
http://php.net/manual/en/function.imagecopyresampled.php
<?php
$width = 3;
$height = 3;
$image = imagecreate($width, $height);
imagecolorallocate($image, 0, 0, 255);
$red = imagecolorallocate($image, 255, 0, 0);
imagesetpixel($image, 0, 0, $red);
imagesetpixel($image, 1, 1, $red);
imagesetpixel($image, 2, 2, $red);
$new_width = 200;
$new_height = 200;
$dst = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($dst, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagepng($dst);
header("Content-Type: image/png");
Related
I want to add a color layer over a image in php using gd.
This is the image:
I want to overlay this with this color: #ABD0D2
I made a quick image how it should look at the end.
Keep in mind that the image should still be transparent
So far I have this code:
$img = imagecreatefrompng('image.png');
imagesavealpha($img, true);
imagefill($img, 0, 0, imagecolorallocatealpha($img, 0, 0, 0, 127));
// make overlay with new color???
imagepng($img, 'new.png');
imagedestroy($img);
You can create a new image, filled with your target colour, and then merge the two:
$img = imagecreatefrompng('image.png');
$w = imagesx($img);
$h = imagesy($img);
imagesavealpha($img, true);
$img2 = imagecreatetruecolor($w, $h);
imagefill($img2, 0, 0, imagecolorallocatealpha($img, 0xAB, 0xD0, 0xD2, 64));
imagecopy($img, $img2, 0, 0, 0, 0, $w, $h);
imagepng($img, 'new.png');
imagedestroy($img);
imagedestroy($img2);
Result:
It's not completely clear to me how you want to maintain transparency (as your expected result image isn't transparent) so in the code above I've set the 'mask' colour at 50% opacity.
That worked for me:
$width = 400;
$height = 400;
$image = imagecreatefrompng('img.png');
$blueOverlay = imagecreatetruecolor($width, $height);
imagesavealpha($image, true);
imagealphablending($image, false);
imagesavealpha($blueOverlay, true);
$blue = imagecolorallocatealpha($blueOverlay, 0, 0, 255, ceil(0.22 * 127));
imagefill($blueOverlay, 0, 0, $blue);
imagecopymerge($blueOverlay, $image, 0, 0, 0, 0, $width, $height, 70);
imagepng($blueOverlay, 'imgWithOverlay.png');
imagedestroy($image);
imagedestroy($blueOverlay);
The code suppose show the image like this below
however the result is this below
this is my code here
simplegraph.php
<?php
// set up image canvas
$height = 200;
$width = 200;
$im = imagecreatetruecolor(width, height)($width, $height);
$white = imagecolorallocate ($im, 255, 255, 255);
$blue = imagecolorallocate ($im, 0, 0, 255);
// draw on image
imagefill($im, 0, 0, $blue);
imageline($im, 0, 0, $width, $height, $white);
imagestring($im, 4, 50, 150, 'Sales', $white);
// output image
header('Content-type: image/png');
imagepng ($im);
// clean up
imagedestroy($im);
?>
here is all files in
htdocs
This is probably because of this statement:
$im = imagecreatetruecolor(width, height)($width, $height);
The above is incorrect syntax. Change this to:
$im = imagecreatetruecolor($width, $height);
The image will show after this, because PHP is assuming, that width and height are constants, but they do not exist and it will give out error, and after that the ($width, $height) part will cause the syntax error. But that is the part that you actually need.
I need to merge a blurry rectangle on another image (a white rectangle). I tried to imagesavealpha() but unfortunately the background of the rectangle remains black, and I want it with a gradient from red to white.
Here is my code:
<?php
$width = 200;
$height = 180;
$bw = $bh = 30;
$img1 = imagecreatetruecolor($width, $height);
$img2 = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($img1, 255, 255, 255);
$red = imagecolorallocate($img2, 255, 0, 0);
imagefilledrectangle($img1, 0, 0, 100, 100, $white);
imagefilledrectangle($img2, 5, 5, 25, 25, $red);
imagefilter($img2, IMG_FILTER_GAUSSIAN_BLUR);
imagefilter($img2, IMG_FILTER_GAUSSIAN_BLUR);
imagefilter($img2, IMG_FILTER_GAUSSIAN_BLUR);
imagefilter($img2, IMG_FILTER_GAUSSIAN_BLUR);
imagefilter($img2, IMG_FILTER_GAUSSIAN_BLUR);
imagesavealpha($img2, true);
imagecopymerge($img1, $img2, 20, 20, 0, 0, $bw, $bh, 100);
header('Content-Type: image/png');
imagepng($img1);
imagedestroy($img1);
The restulting image is:
If you just want a blurred red rectangle on a white background your code can be simplified to this:
<?php
$width = 200;
$height = 180;
$img = imagecreatetruecolor($width, $height);
// fill with opaque white.
imagefill($img, 0, 0, 0x00ffffff);
// draw rectangle in opaque red.
imagefilledrectangle($img, 5, 5, 25, 25, 0x00ff00000);
for ($i = 0; $i < 5; $i++) {
imagefilter($img, IMG_FILTER_GAUSSIAN_BLUR);
}
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
Result (of course the white background blends with the page background...):
If you want to be able to blend the red rectangle with any background colour (full alpha blending) then you might be out of luck. As far as I can tell IMG_FILTER_GAUSSIAN_BLUR doesn't support alpha values (I'm using PHP 7.0.3).
I have this script, which makes an image and posts it on another image:
<?php
$img=imagecreatetruecolor(150,20);
imagealphablending($img,false);
$col=imagecolorallocatealpha($img,255,255,255,127);
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
imagefilledrectangle($img,0,0,180,20,$col);
imagealphablending($img,true);
$font='../ttf/0001.ttf';
$color = imagecolorallocate($img, 0, 0, 0);
imagettftext($img,11,0,5,14,$color,$font,'Text goes here');
imagealphablending($img,false);
imagesavealpha($img,true);
imagejpeg($img, '../custom_images/test.jpg');
// Create image instances
$dest = imagecreatefromjpeg('../custom_images/121536.jpg');
$src = imagecreatefromjpeg('../custom_images/test.jpg');
$width = imagesx($src);
$height = imagesy($src);
imageantialias($src, true);
$color = imagecolorallocatealpha($src, 0, 0, 0, 127);
$rotated = imagerotate($src, 0, $color);
imagesavealpha($rotated, true);
$trans_colour = imagecolorallocatealpha($rotated, 0, 0, 0, 127);
imagefill($rotated, 0, 0, $trans_colour);
imagepng($rotated, 'shahid.png');
$new_img = imagecreatefrompng('shahid.png');
$width = imagesx($new_img);
$height = imagesy($new_img);
// imagecopymerge($dest, $new_img, 50, 50, 0, 0, $width+60, $height+60, 100);
imagecopymerge_alpha($dest, $new_img, 0, 20, 0, 0, $width, $height, 100);
// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
?>
2 things:
Background is not transparent
I want the Width and Height to be automatic, so if the text is short, the image is it to.
What do I fault?
I am using PHP GD imagecopy or imagecopyresampled to merge a PNG image with transparency onto another PNG image (basically creating t-shirt mockups with design).
When I use imagecopymerge() it doesn't respect the transparency. but opacity setting option works. But when I use imagecopy() or imagecopyresampled() then transparency works but opacity setting is not available.
So how to merge images which respects transparency and provides 50% opacity as well?
My code is:
$img1 = imagecreatefrompng('m1.png');
$img2 = imagecreatefrompng('m2.png');
imagealphablending( $img2, false );
imagesavealpha( $img2, true );
$x1 = imagesx($img1);
$y1 = imagesy($img1);
$x2 = imagesx($img2);
$y2 = imagesy($img2);
//imagecopyresampled($img1, $img2, 205, 170, 0, 0, $x2-40, $y2-40, $x2, $y2);
imagecopy($img1, $img2, 205, 170, 0, 0, $x2-40, $y2-40);
header('Content-Type: image/png');
imagepng($img1);
Please help.
You can have a look at this link:
https://bugs.php.net/bug.php?id=23815
imagecopymerge doesn't support image alpha. Becuase of that, they have requested the creation of a new function imagecopymergealpha to do this kind of job.
https://github.com/php/php-src/pull/211
imagecopymerge($img1, $img2, 0, 0, 0, 0, $x1, $y1, 50);
header('Content-Type: image/png');
imagepng($img1);
Please check using this sample code
$imageName = 'path/to/your/image/file'
$im_src = create_image_from_type($imageName);
$size = getimagesize($imageName);
$im_dst = create_image_from_type($imageName);
$white = imagecolorallocate($im_dst, 255, 255, 255);
imagecolortransparent($im_dst, $white);
imagefilledrectangle($im_dst, 0, 0, $size[0], $size[1], $white);
$opacityVal = 50;// put the opacity value here
imagecopymerge($im_dst, $im_src, 0, 0, 0, 0,$size[0], $size[1], $opacityVal);
save_image($im_dst, $imageName, 100);