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');
Related
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 a chart with PHP and GD. The chart is already working but I
can't get the background to be transparent (with anti-aliasing).
I want to place an anti-aliased line on a gradient background (this is the HTML background) but it shows some white pixels (see the image link and my code below). Is it possible to do this with GD? I have searched a lot on the Internet but can't find any solutions.
PHP
<?php
$img = imagecreatetruecolor(1000, 1000);
imagesavealpha($img, true);
imagealphablending($img, true);
$color = imagecolorallocatealpha($img, 255, 255, 255, 127);
$red = imagecolorallocate($img, 255, 0, 0);
imagefill($img, 0, 0, $color);
imageantialias($img, true);
imageline($img, 10, 10, 500, 20, $red);
header("content-type: image/png");
imagepng($img);
imagedestroy($img);
?>
HTML
<style type="text/css">
body{
background:url('/Image/background.png');
}
</style>
<img src="./example.php" />
The PHP manual basically states that you cannot:
It does not support alpha components. It works using a direct blend
operation. It works only with truecolor images.
Thickness and styled are not supported.
Using antialiased primitives with transparent background color can end
with some unexpected results. The blend method uses the background
color as any other colors. The lack of alpha component support does
not allow an alpha based antialiasing method.
http://es.php.net/imageantialias
So unless someone comes with third-party code or some witty hacks, you're out of luck.
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.
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
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.