I have a simple facebook aplication. I have the index.php and the image.php which when accessed it creates an image with a random text on it(at least this is what i want to get from the script).
Here is the image.php:
http://pastebin.com/B4JhHcfj
The script is incomplete because i don't know how to get the text on the generated image.
Example: I access the image.php, i get a image with a random text on it.If i access again the link the same image with the random text on it will be shown.Thanks
Ok , you can use GD to watermark images , the complete script for watermarking :
<?php
header('content-type: image/jpeg');
$watermark = imagecreatefrompng('watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($_GET['src']);
$size = getimagesize($_GET['src']);
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
please note that , this script create watermarks on the fly , if you want to save watermarked image you must use imagejpeg() function with this way :
imagejpeg($image,'watermarked_img.jpg');
and you also use .htaccess to redirect all image request to your watermark.php,create .htaccess file and paste this code on it :
RewriteEngine on
RewriteRule ^([^thumb].*\.[jJ].*)$ watermark.php?src=$1
good luck.
I presume from your question, you have already included GD and that your PHP script generates the graphic using GD.
Firstly, you have to ensure that the required fonts are installed in the folder(s) where PHP will look for them.
Then, you should take a look at the following function, ImageTTFText:
http://php.net/manual/en/function.imagettftext.php
I did a PHP class to make it easier:
http://alvarotrigo.com/blog/using-php-gd-library-to-write-text-over-images-using-truetype-fonts/#comment-1048371709
It works with true type fonts (ttf) to be able to write text in big font sizes.
Related
I am trying to create an employee poster for a website that I develop. My goal is to take an image from a directory on the server of any file type (.png, .gif, .jpeg, etc.) and copy it onto another generate imaged that will then be outputted to the browser.
The problem is that I use:
$final_image = imagecreatefrompng("large_background.png");
for making the final image and for some reason if I add profile images with the type jpeg's, gif's, etc, (any type that isn't a jpeg) it doesn't work. The images never show up in the output. However, if I use png's it does work.
To solve this problem I tried converting the image to a png and then creating a png from it as shown in the code below. Unfortunately it doesn't work. Profile images still do not show up on the background.
// get image from database
$image_from_database = could be a .png, .jpeg, .gif, etc.
// get the image from the profile images directory
$path = "profile_images/".$image_from_database;
// create a png out of the image
$image = imagecreatefrompng(imagepng($path));
// add the $image to my larger $final_image (which is a png)
imagecopy($final_image, $image, $x, $y, 0,0, $height, $width);
imagepng($final_image, $ouput_url);
...
Can anybody tell me why this won't work? My profile images do not show up in the output of the final image.
My questions,
Is this line imagecreatefrompng(imagepng(...)); even possible? Essentially I want to convert an image of any type into a png and then create a png from it.
I'm just running a few local tests... The following works:
$src = imagecreatefromgif('test.gif');
$dest = imagecreatefrompng('test.png');
imagecopy($dest, $src, 0, 0, 0, 0, 100, 100);
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($src);
imagedestroy($dest);
So does this:
$src = imagecreatefromstring(file_get_contents('test.gif'));
If you're still having trouble after trying the latter example, please update your question. The actual images you're using would be helpful, in addition to a functional code example.
After you read an image with a imagecreatefrom* function, it doesn't matter what format the original was.
imagecreatefrom* functions return an image resource. When the image is loaded you are using internal representation of the images and not PNG, JPEG or GIF images.
If the images are successfully loaded imagecopy should have no problems with them.
This code uses images in different formats and works without a problem:
$img = imagecreatefrompng('bg.png');
$png_img = imagecreatefrompng('img.png');
$jpeg_img = imagecreatefromjpeg('img.jpeg');
$gif_img = imagecreatefromgif('img.gif');
/* or use this, so you don't need to figure out which imagecreatefrom* function to use
$img = imagecreatefromstring(file_get_contents('bg.png'));
$png_img = imagecreatefromstring(file_get_contents('img.png'));
$jpeg_img = imagecreatefromstring(file_get_contents('img.jpeg'));
$gif_img = imagecreatefromstring(file_get_contents('img.gif'));
*/
imagecopyresampled($img, $png_img, 10, 10, 0,0, 100, 100, 200, 200);
imagecopyresampled($img, $jpeg_img, 120, 10, 0,0, 100, 100, 200, 200);
imagecopyresampled($img, $gif_img, 230, 10, 0,0, 100, 100, 200, 200);
header('Content-Type: image/png');
imagepng($img);
Your example
$image = imagecreatefrompng(imagepng($path));
is wrong.
imagepng is used to output an image resource as a PNG image. If you provide a path as a second argument a PNG image file is created, otherwise it's printed to the output like echo does.
What imagepng actually returns is a boolean, indicating, if the output was successful.
You are then passing that boolean to imagecreatefrompng which expects a filepath. This is obviously wrong.
I suspect you have a problem with loading images.
imagecreatefrom* functions return FALSE on failure and you should check, if you have any problems with that.
Maybe your image paths are relative to doc root and your working directory is different.
Or you have permission problem.
Or your images are just missing.
It's impossible to tell from your question.
I posted earlier about another resizing script not working and I got a little farther with this script which does things a little differently.
I got a little farther, only now there is a new problem. The first three lines of the code successfully place three identical files in the target directory with the file and it's two thumbnail files named accordingly. I then want to load the thumbnails, which are still full-size, and resize them but the script stops at imagecreatefromjpeg() and I can't seem to figure out why because $src has a value.
I thought that I could possibly remove that line and replace $source with $src in my imagecopyresized() function, and that gets me even closer. But it then returns a thumbnail of the target size, but the thumbnail is black.
move_uploaded_file($tmpFilePath, $newFilePath);
copy($newFilePath, $thumb500);
copy($newFilePath, $thumb200);
function thumbImage($src, $dest, $newheight) {
list($width, $height) = getimagesize($src);
$newwidth = $width * ($newheight / $height);
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
header('Content-type: image/jpeg');
$source = imagecreatefromjpeg($src);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb, $dest);
}
thumbImage($thumb500, $thumb500, 500);
thumbImage($thumb200, $thumb200, 200);
I feel as if this must be a common issue. Any suggestions anyone?
For me the supplied code block works if I use a JPEG image as source.
The problem may be that you use a PNG image that uses transparency. As JPEG can not handle transparencies, the transparent background colour will be filled. Maybe that is the problem. If not, please provide a sample image that has the problematic behaviour.
5
imagecopyresized takes an image resource as its second parameter, not a file name. You'll need to load the file first. If you know the file type, you can use imagecreatefromFILETYPE to load it. For example, if it's a JPEG, use imagecreatefromjpeg and pass that the file name - this will return an image resource.
If you don't know the file type, all is not lost. You can read the file in as a string and use imagecreatefromstring (which detects file types automatically) to load it as follows:
{$oldImage = imagecreatefromstring(file_get_contents($_FILES['image']['tmp_name']));
}
enter code here
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);
?>
I am testing a script where I watermark an image in my webpage.
The script works fine and the image is watermark but my problem is that only the image is displayed on the page.
As soon as I add the script to my page it's like the web page is converted to the image that I'm watermarking.
I think it's because of header("content-type: image/jpeg"); from the code.
I need to watermark the image on my webpage but I also need the rest of my webpage to be displayed too.
How is this done? I'm quite confused on how this works.
The script I'm using is from here
Here's the code I'm using:
<?php
$main_img = "Porsche_911_996_Carrera_4S.jpg"; // main big photo / picture
$watermark_img = "watermark.gif"; // use GIF or PNG, JPEG has no tranparency support
$padding = 3; // distance to border in pixels for watermark image
$opacity = 100; // image opacity for transparent watermark
$watermark = imagecreatefromgif($watermark_img); // create watermark
$image = imagecreatefromjpeg($main_img); // create main graphic
if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");
$watermark_size = getimagesize($watermark_img);
$watermark_width = $watermark_size[0];
$watermark_height = $watermark_size[1];
$image_size = getimagesize($main_img);
$dest_x = $image_size[0] - $watermark_width - $padding;
$dest_y = $image_size[1] - $watermark_height - $padding;
// copy watermark on main image
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
// print image to screen
header("content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
NOTE: I'm getting the image path from the database so I cannot hardcode the image filename as it's dynamic.
This is a bit mind-boggling if you're doing it for the first time, but once you see how it works it's really simple. ;)
You need one script that generates the image (e.g. image.php) and then your main script references that image (e.g. <img src="image.php">). It's not possible to have one request/script return both a document and an image simultaneously.
(PS. it's actually possible but it involves encoding the image as a very weird kind of src attribute. You don't want to do it, trust me.)
I believe you got confused about how this works: The watermark generating code above is not meant to be included in the page generating scripts/logic. Instead, it needs to be accessible behind it's own URL and then linked via <img src="./path/to/script"/> into the site you want to contain that image.
Put this script entirely separated from your "main website" script. On your website, use an <img> tag, with that watermarking script as the source.
You can't combine two different content-types like you're appearing to do. If you want a "web page" to appear, the content-type needs to be text/html. If you want just an image to appear, the content-type needs to be image/*.
I need to create a page that will automatically create a thumbnail from all images if selected folder and show them on the same page. But the thing is, I don't want to save them. I want to display them on the same page without saving. And I need to do that in PHP. Is it possible? Please help!
Note: If that is not possible,I can put them in some folder.
You have to make a second PHP-file then. You have to make an image tag in the first file <img src='image.php' /> and in this image.php you have to print out the image like:
header("Content-type: image/png");
$im = imagecreatefrompng("image.png");
list($width, $height) = getimagesize($im);
$newimage = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($newimage, $im, 0, 0, 0, 0, "100", "100", $width, $height);
imagepng($newimage);
imagedestroy($newimage);
imagedestroy($im);
You might also consider using data URIs. Use the code in DRP96's answer to create the thumbnail, but instead of doing it in a separate PHP and requiring many image requests, embed the images directly on the page.
Most modern browsers support data URIs now, but as usual, watch out for IE.
replace this line
list($width, $height) = getimagesize($im);
with this
list($width, $height) = getimagesize("image.png");