imagefill only filling half the image background? - php

imagefill is only filling half the image background?
Anyone got any ideas?
$img = imagecreatefrompng($current);
$kek=imagecolorallocate($img, 255, 255, 255);
imagefill($img,0,0,$kek);
imagejpeg($img,$new,70);
imagedestroy($img);

imagefill perform a Flood-Fill algorithm to the image, same as bucket fill in paint does. so filling continues until the area is closed, and filling will be stopped.
You should copy original image into larger canvas, then perform an imagefill, then crop the image agian to get back to original size:
$img = imagecreatefrompng($current);
$width=imagesx($img);
$height=imagesy($img);
$newimg=imagecreatetruecolor($width+2,$height+2);//1 pixcel larger from each side
imagecopy($newimg,$img,1,1,0,0,$width,$height);
$kek=imagecolorallocate($img, 255, 255, 255);
imagefill($img,0,0,$kek);
imagecopy($img,$newimg,0,0,1,1,$width-2,$height-2);//back to the original size
imagejpeg($img,$new,70);
I didn't test the code, but I think it should be OK.

Related

PHP convert to transparent doesn't literally make it transparent

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 )

Resizing images with `imagecopyresampled` from PHP GD

Using the PHP GD library, I generate image resource that is opaque white, with some "holes" here and there, which are fully transparent disk shapes. I've attached it bellow, although you'll need to first save it and open it (in Windows) with the Image Preview application in order to see the holes in it, due to the blue-ish background. Otherwise you'll only see white.
The original image:
From that image resource, which is 2550 x 3000px, I need to create a smaller version. I do so using imagecopyresampled(). All is fine with the resulting image, with one exception: here and there, it contains grey pixels (RGB: 254,254,254):
The resized image:
Part of the code I use is bellow:
$previewPxWidth = $this->viewportWidth_;
$previewPxHeight = round($this->viewportWidth_ / $schematic['paper.aspect.ratio']);
$preview = imagecreatetruecolor($previewPxWidth, $previewPxHeight);
$noColor = imagecolorallocatealpha($preview, 255, 255, 255, 127);
imagesavealpha($preview,true);
imagefill($preview, 0, 0, $noColor);
imagecopyresampled($preview, $sheet, 0, 0, 0, 0, $previewPxWidth, $previewPxHeight, $sheetPxWidth, $sheetPxHeight);
imagedestroy($sheet);
header("Content-type: image/png");
header("Content-disposition: inline; filename=image.png");
imagepng($preview);
Where are those very light and (apparently) randomly positioned grey pixels coming from and how can I get rid of them?
Try imageAlphaBlending(), but ImageMagic is best way.

Image Randomization using GD library

I have been tasked with a problem where i need to pickup one image froma set of 1000 and serve it to the user based on the parameters passed in the get query.
This was simple.
Adiditonally, i have been asked to serve the image in such a way that even if the same file is server everytime, the sha1 hash of the image file should come different.
To achieve this,We could just add random pixels in the image background at random places.
Can somebody tell me how i can acheive this using the GD library
Use Imagesetpixel.
$img = imagecreatefrompng('your_image.png');
$red = imagecolorallocate($img, 255, 0, 0);
imagesetpixel($img, $x, $y, $red);
........
........
On the other hand why would you want to change the sha1 hash of the image on every request?
Edit: Since you want a transparent pixel, you are going to need imagealphablending and something like this:
$img = imagecreatefrompng('your_image.png');
imagealphablending($img, false);
$transparent = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagesetpixel($img, $x, $y, $transparent);
imagesavealpha($img, true);
imagepng($img, 'my_saved_file.png');

Heroku create image

im using php and heroku to create some images for my facebook app, but the images arent shown, only picture of an broken image is shown.
Im using sample code from php tutorial website.
<?php
// Create a 100*30 image
$im = imagecreate(100, 30);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left
imagestring($im, 5, 0, 0, 'Hello world!', $textcolor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
The code is in located in /src/texttopic.php
Heroku logs shows no errors.
Call the image like so:
<img src="/src/texttopic.php" height="30" width="30">
Set the image height / width accordingly.
Keep in mind that with heroku, if you have 2 or more web dynos running, the image creation may take place on one dyno, but when you request the image, you may be calling another dyno that does not have that image.

Insert Image file into php's imagecreate

I'm trying to dynamically generate an image file that will be composed by other smaller images. This is the code I've got so far
if ( function_exists('imagecreate') ) {
header ("Content-type: image/png");
$im = #imagecreate ($imgWidth, $imgHeight)
or die ("Cannot initialize new GD image stream");
$background_color = imagecolorallocate ($im, 255, 255, 255);
$text_color = imagecolorallocate ($im, 0,0,0);
imagepng ($im);
imagedestroy($im);
}
So far, it's working fine. I just can't figure out how to include my smaller images into this new, bigger, dynamically generated image. Does anyone know how to do this? Ideally, the function that does this (or whatever it is) would ask me for the image file, it's x and y positions in the second image file.
Any suggestions? I sincerely appreciate any help you can bring me. Thank you so so much!!
Imagecopymerge is probably what you are looking for

Categories