I was unable to watermark my images. I used the php manual found at this link
http://www.php.net/manual/en/image.examples-watermark.php
And also tried the sitepoint tutorial here
http://www.sitepoint.com/watermark-images-php/
but getting the same error that the image cannot be displayed because its have some errors. can somebody let me know whats wrong with the code or suggest me some better solution.
My code is here :
header('content-type: image/jpeg');
$watermark = imagecreatefrompng('wm.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg('img.jpg');
$size = getimagesize('img.jpg');
$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);
Check all the return values of the functions you call, e.g.
$watermark = imagecreatefrompng('wm.png');
if ( !$watermark ) {
die('Sorry ' . __LINE__); // you might want to use something else here - just an example....
}
and then set the content type to image/jpeg not before you're actually trying to send the image
if ( headers_sent($file, $line) ) {
die('oops '.__LINE__);
}
else {
header('content-type: image/jpeg');
imagejpeg($image);
}
...makes it easier to pin-point the error.
Related
i have script for gallery. He takes image and shows it on that image url. But its not working, images wont show up on gallery pages. Everything else works. Database is okay, so are files on FTP. Any idea? Here is code of that scrip (config.php is okay too)
<?
require ('config.php');
$img = $_GET['img'];
if (!isset($img) or (!is_numeric($img)))
{ $masterURL = 'images/no_image.jpg'; }
else
{
$fotosql = MySQL_Query ("SELECT photo FROM photo WHERE id = '$img' LIMIT
1");
$foto_file = MySQL_Result ($fotosql, 0, 'photo');
$masterURL = 'photos/$foto_file';
}
header('content-type: image/png');
$watermark = imagecreatefrompng('images/watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($masterURL);
$size = getimagesize($masterURL);
$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);
imagepng($image);
imagedestroy($image);
imagedestroy($watermark);
?>
I am trying to install a watermark in the middle of my image, but every time it shows a weird square which is not fully transparent. This is the result of my code:
This is my code:
<?php
header("Content-type: image/png");
$image = imagecreatefromjpeg('http://www.sideshowtoy.com/wp-content/uploads/2016/03/dc-comics-batman-v-superman-woner-woman-sixth-scale-hot-toys-feature-902687.jpg');
$watermark = imagecreatefrompng('https://d5odq6jbm6umf.cloudfront.net/assets/img/video-play-button-transparent.png');
imagesavealpha($watermark,true);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$dest_x = (imagesx($image) - $watermark_width)/2;
$dest_y = (imagesy($image) - $watermark_height)/2;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
I usually create a new true colour resource and copy everything into it. This ensures GD doesn't get too quirky. It's a little bit more resource intensive, but should be negligible for most cases.
Below is your code modified to create a new image, copy in the jpeg, and then overlay the partially transparent watermark:
<?php
$image = imagecreatefromjpeg('http://www.sideshowtoy.com/wp-content/uploads/2016/03/dc-comics-batman-v-superman-woner-woman-sixth-scale-hot-toys-feature-902687.jpg');
$img_w = imagesx($image);
$img_h = imagesy($image);
$new = imagecreatetruecolor($img_w, $img_h);
imagecopy($new, $image, 0, 0, 0, 0, $img_w, $img_h);
imagedestroy($image);
$watermark = imagecreatefrompng('https://d5odq6jbm6umf.cloudfront.net/assets/img/video-play-button-transparent.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$dest_x = ($img_w - $watermark_width) / 2;
$dest_y = ($img_h - $watermark_height) / 2;
imagecopy($new, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
header('Content-type: image/png');
imagejpeg($new);
imagedestroy($new);
imagedestroy($watermark);
Result:
Am developing new photography website in which images are uploaded frequently.... Now I want to add watermark on uploaded images before it goes to client side, I have used the following code for watermark
$SourceFile = "sys\img\image1.jpg";
$DestinationFile = "sys\img\image1-wm.jpg";
$WaterMarkText = 'Copyright sys.com';
watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile);
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);
};
But it showing the following error
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your
request.
Please contact the server administrator to inform of the time the error occurred and of anything
you might have done that may have caused the error.
More information about this error may be available in the server error log.
What is the mistake I have done...?
Finally I found an another way to add watermark to my images...
Here is the code for watermark....
$filename=$_REQUEST['filename'];
$imgpath="img/"; // Folder of your images in the server
$imgpath = $imgpath.$filename;
header('content-type: image/jpeg'); //HTTP header - assumes your images in the gallery are JPGs
$watermarkfile="img/watermark.png";
$watermark = imagecreatefrompng($watermarkfile);
list($watermark_width,$watermark_height) = getimagesize($watermarkfile);
$image = imagecreatefromjpeg($imgpath);
$size = getimagesize($imgpath);
$dest_x = $size[0] - $watermark_width - 15;
$dest_y = $size[1] - $watermark_height - 15;
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
Save this code as watermark.php and then use it in the img tag as follows...
<img src="watermark.php?filename=image1.jpg">
Thats it... Working fine...
I've got this script, which only saves the image at $image, and not the image at $newimage_2. Help?
<?php
$newimage_1 = imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
$newimage_2 = imagecreatefromjpeg($newimage_1);
// print image to screen
header("content-type: image/jpeg");
imagejpeg($image);
imagejpeg($newimage_2);
imagedestroy($image);
imagedestroy($watermark);
imagedestroy($newimage_2);
?>
$source_file_path=$_FILES["image"]["tmp_name"];
$src = imagecreatefromjpeg($source_file_path);
list($width,$height)=getimagesize($source_file_path);
$newwidth=540;
$newheight=round(($height/$width)*$newwidth);
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$target_file_path = "images/".$filewhereyouwanttosaveit;
$watermark = imagecreatefrompng('imgs/copyright.png');
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = ($newwidth - $watermarkwidth);
$startheight = ($newheight - $watermarkheight);
imagecopy($tmp,$watermark,$startwidth,$startheight,0,0,$watermarkwidth,$watermarkheight);
imagegif($tmp,$target_file_path);
you probably dont need the resizing but code may help you...
imagegif or jpg or png or some else
Here is a link to the page http://www.true-gamerz.net/test2.php
if you look at the image, The rank image is transpent but it dose not show it on the screen
dose anyone know why this is happen?
Here is the code
<?
header('content-type: image/png');
$watermark = imagecreatefrompng('images/ranks/ranks_18.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefrompng("images/card/test.png");
$size = getimagesize("images/card/test.png");
$dest_x = $watermark_width;
$dest_y = $watermark_height;
imagecopymerge($image, $watermark, 289, 4, 0, 0, $watermark_width, $watermark_height, 100);
imagepng($image);
imagedestroy($image);
imagedestroy($watermark);
?>
imagecopymerge does not support alpha channel
Read this for workaround:
http://www.php.net/manual/en/function.imagecopymerge.php#92787