Insert Image file into php's imagecreate - php

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

Related

IMG_FILTER_COLORIZE not working as it should

I am trying to turn a PNG image with a white box to a green box. The background of the PNG is transparent.
I have written out code but it doesn't work for some reason. I have looked and tried a lot of things and i cannot see anything wrong with the code.
Thanks
header("Content-type: image/png");
$image = imagecreatefrompng($filename);
imagefilter($image, IMG_FILTER_COLORIZE, 0, 255, 0, 100);
imagesavealpha($image, TRUE);
imagepng($image, $save_filename);
The image gets exported still with a white box. Will IMG_FILTER_COLORIZE not work with white?
Thanks
I have fixed this issue. To anyone in the future...
I added
imagefilter($image, IMG_FILTER_NEGATE);
Before image_filter(

imagefill only filling half the image background?

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.

Automatically adding watermark on image download

Is it possible to add a watermark when someone downloads an image from your website? If yes, what's the best way to do it?
Thanks in advance.
If you mean when Right Click -> Saveing it, thats not possible I'm afraid.
If you generally mean that you have a dedicated download button or link, You could make It would redirect the request through a PHP file that will add the needed watermark and generate a new image file for download.
I have a better idea.
Since you said you wanna protect stuff when people Right Click and Select Save As. So, we can use the way how 9gag does.
Create an image with a fixed size of footer. Use a negative margin parent of the size of the footer of the bottom margin. Give overflow: hidden; so that the users cannot see the watermark, which is hidden from the view. Now when the users right click and save as image, they will have the watermark. Altogether, there is no place where the image is without watermark. So, while uploading the image, use the above said techniques to add the watermark.
Or, if you would like to make separate watermarked images, then you can check the hotlinked files and then serve watermarked images.
header("content-type: image/jpeg");
if (!isset($_SERVER['HTTP_REFERER'])){die("alert('Restricted Access!');");};
$_u=parse_url($_SERVER['HTTP_REFERER']);
$_u=preg_replace("/(www.)/i","",strtolower($_u['host']));
$_i=$_SERVER['HTTP_HOST'];
$_i=preg_replace("/(www.)/i","",strtolower($_i));
if ($_u != $_i){
//handle this with gd or redirect
}
Follow the instructions in this tutorial to make the watermark on the picture.
I would suggest using a function imagecopymerge() to ad watermarks in php http://www.php.net/manual/en/function.imagecopymerge.php but as mentioned: they should be added before loading them in the browser. When user downloads them (right-click) then they're already served to their browser (and are usually in cache).
Ofcourse you could serve all images dynamically and check the http_referer on image load. And if that's missing or not an expected one (file isn't being loaded from your webpage) then add a water-mark but that's not foolproof.
Please check following url same in this site , this ll help you lot
http://www.phpjabbers.com/put-watermark-on-images-using-php-php20.html
Following are from the above link
<?php
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$font = 'arial.ttf';
$font_size = 10;
imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);
if ($DestinationFile<>'') {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
};
?>
<?php
$SourceFile = 'image1.jpg';//image path
$DestinationFile = 'images/image1-watermark.jpg'; //Out put path
$WaterMarkText = 'Copyright Watermark text';
watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile);
?>

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.

Categories