Have some script that display images on page with png watermark, but when i want to fade watermark png image to 50% there is white background display (but watermark fading to 50%) and when i want to change the size of watermark image the watermark png just gone and not displayed. Please help me to solve my problemwith fading and size changing. Ертл you to all.
<?php
/* Image source */
$src = strtolower($_GET['src']);
/* If no image src redirect to start page */
if(isset($src) && !empty($src) && $src !=0 && $src !=1) {
header('content-type: image/jpeg');
$path = pathinfo($src);
$watermark = imagecreatefrompng('karantino.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
if ($path["extension"]=="png") {
$image = "no_img.jpg";
}else if ($path["extension"] == "jpg" || $path["extension"] == "jpeg"){
$image = imagecreatefromjpeg($src);
$size = getimagesize($src);
$dest_x = $size[0] - $watermark_width-10;
$dest_y = $size[1] - $watermark_height-10;
$alpha_channel = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagecolortransparent($image, $alpha_channel);
imagefill($image, 0, 0, $alpha_channel);
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagesavealpha($image);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
}
} else{
header("HTTP/1.1 301 Moved Permanently");
header ("Location: /");
}
/*
to use this code, just create php file with random name (i use wt.php) and put code like this in your HTML page <img src="wt.php?src=your_jpg_file.jpg">
*/
?>
Try to change image type to png (not the watermark image). Because the default background in jpg is white.
And about the size : change the image original size not in code.
I find a new working suggestion for me:
<?php
header("Content-type:image/jpeg");
$src = $_GET["src"];
/* If no image src redirect to start page, else do watermark */
if(isset($src) && !empty($src) && $src!=0 && preg_match("/.(jpg|jpeg|png)$/", $src)){
$path = pathinfo($src);
if ($path["extension"] == "png") {
$stamp = imagecreatefrompng("../../favicon.png");
$im = imagecreatefrompng($src);
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right,
imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp),
imagesy($stamp));
imagepng($im);
imagedestroy($im);
}
else if ($path["extension"] == "jpg" || $path["extension"] == "jpeg"){
$stamp = imagecreatefrompng("../../favicon.png");
$im = imagecreatefromjpeg($src);
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right,
imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp),
imagesy($stamp));
imagepng($im);
imagedestroy($im);
}
else{
/* If unknow format */
$im = imagecreatefrompng("../important/no-photo.png");
imagepng($im);
imagedestroy($im);
}
}
else{
/* Redirect to start page if no string */
header("HTTP/1.1 301 Moved Permanently");
header ("Location: /");
}
/*
to use this code, just create php file with random name and put code like this in your HTML page <img src="wt.php?src=your_jpg_file.jpg">
*/
?>
But still have a problem. When i use code like this
<img src="wt.php?src=/img/my_img/your_jpg_file.jpg"> instead of this <img src="wt.php?src=your_jpg_file.jpg"> the script didn't display any image.
Any ideas?
Related
I am trying to create watermark on images programmatically in PHP. Images are uploaded by users and they can be of different dimensions. What I am doing to create a watermark is this:
$stamp = imagecreatefrompng('stamp381x387.png');
$ext = substr(strrchr($_GET['src'], '.'), 1);
if ($ext == 'png') {
$im = imagecreatefrompng($_GET['src']);
} else if ($ext == 'jpg') {
$im = imagecreatefromjpeg($_GET['src']);
}
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 0;
$marge_bottom = 0;
$sx = imagesx($stamp); //width
$sy = imagesy($stamp); //height
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, (imagesx($im) - $sx ) / 2, (imagesy($im) - $sy) / 2, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
if ($ext == 'png') {
header('Content-type: image/png');
imagejpeg($im);
imagedestroy($im);
} else if ($ext == 'jpg') {
header('Content-type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
}
Now, it creates water mark on images but those are not uniform. See attached Images.
How to make it uniform on all images? any help?
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
Here's a good example for adding watermark on images using PHP -
http://php.net/manual/en/image.examples-watermark.php
I am wanting to resize and add watermark to the uploaded image.
I am able to resize the image successfully but once I apply the watermark, the watermark has a black background rather than transparent.
$watermark_l = "source/watermark_l.png";
$size_wm_l = getimagesize($watermark_l);
$watermark_l = imagecreatefrompng($watermark_l);
$filename = "input/$gallery/$file";
header('Content-Type: image/jpeg');
list($width, $height) = getimagesize($filename);
$x_large = 2000;
$y_large = 1333;
$image_p = imagecreatetruecolor($x_large, $y_large);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $x_large, $y_large, $width, $height);
imagecopy($image_p, $watermark_l, 0, 0, 0, 0, $size_wm_l[0], $size_wm_l[1]);
imagejpeg($image_p, "output/$gallery/2000x1333_$file", 100);
Try to enable alpha blending on images after ImageCreate's:
ImageAlphaBlending($watermark_l,true);
ImageAlphaBlending($image_p,true);
Here's a good example for adding watermark on images using PHP -
<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Source - http://php.net/manual/en/image.examples-watermark.php
So I am watermarking the png image on Gif image.here is my code :
<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('add_item.png');// watermark image
$im = imagecreatefromjpeg('gif_image.gif');// source image
$image_path = "/opt/lampp/htdocs/my/Harshal/watermarking/".time().".png";
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
//header('Content-type: image/png');
imagepng($im,$image_path);
imagedestroy($im);
?>
My code is working fine and its watermarking the png image on gif image but The problem is :
There is white background coming just behind of the watermark png image and not the transparent as comes for other images.
Can you guys tell me why only gif image have issues ?
See the generated image.
I know Gif is the combinations of many images but is there any solution that we can put the image with normal behavior.?
I saw some Online watermarking tools also but they have also same issue.
You can convert the gif to a true color image. Try this:
<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('add_item.png');// watermark image
$im = imagecreatefromgif('gif_image.gif');// source image
$image_path = "/opt/lampp/htdocs/my/Harshal/watermarking/".time().".png";
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Convert gif to a true color image
$tmp = imagecreatetruecolor(imagesx($im), imagesy($im));
$bg = imagecolorallocate($tmp, 255, 255, 255);
imagefill($tmp, 0, 0, $bg);
imagecopy($tmp, $im, 0, 0, 0, 0, imagesx($im), imagesy($im));
$im = $tmp;
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
//header('Content-type: image/png');
imagepng($im,$image_path);
imagedestroy($im);
result: http://i.stack.imgur.com/p1kVz.png
I'm trying to copy PNG to another PNG but I have no idea why they return like this
<?php // File and new size
$filename = 'watermark.png';
// Get new sizes
list($width, $height) = getimagesize($filename);
// Load
$resim = 'http://www.petrominpng.com.pg/images/logo_big.png';
$ext = end(explode('.', $resim));
if($ext == "jpeg" || $ext == "jpg")
{
$thumb = imagecreatefromjpeg($resim);
}
else if($ext == "png")
{
$thumb = imagecreatefrompng($resim);
}
$sx = imagesx($thumb);
$sy = imagesy($thumb);
if($sx >= $sy)
{
$sxd = $sx/2;
$degisim = $sxd/$width;
/*
echo $sxd." ".$width." ";
echo $sxd-$width." |";
*/
$sxy = $height * $degisim;
/*
echo " $sxy $height | $degisim";
exit();
*/
}
else
{
$sxy = $sy/2;
$degisim = $sxy/$height;
/*
echo $sxd." ".$width." ";
echo $sxd-$width." |";
*/
$sxd = $width * $degisim;
/*
echo " $sxy $height | $degisim";
exit();
*/
}
$source = imagecreatefrompng($filename);
// Resize
imagecopyresized($thumb, $source, $sx/5, $sy/4, 0, 0, $sxd, $sxy, $width, $height);
// Output
header('Content-type: image/png');
imagepng($thumb);
imagedestroy($thumb);
?>
You can see that, i have problem with images how can i make it right ?
my watermark
http://i.stack.imgur.com/TZFCa.png
Your code works fine, it appears that something is wrong with the base PNG image (not the watermark) because if you try the code with another png, it works fine, with a jpg, it also works fine.
It seems to be because the original PNG is a PNG8, because when converted to a PNG32 it works fine.
You may try this. It works fine in my project.
$stamp = imagecreatefrompng('watermark.png');
$im = imagecreatefrompng('source.png');
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 1;
$marge_bottom = 1;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
// OUTPUT IMAGE:
header("Content-Type: image/png");
imagesavealpha($im, true);
imagepng($im, NULL);
The watermark image should be in one of the following recommended
formats:
PNG-8 (recommended)
Colors: 256 or less
Transparency: On/Off
GIF
Colors: 256 or less
Transparency: On/Off
JPEG
Colors: True color
Transparency: n/a
The imagecopymerge function does not properly handle the PNG-24
images; it is therefore not recommended.
If you are using Adobe Photoshop to create watermark images, it is
recommended that you use "Save for Web" command with the following
settings:
File Format: PNG-8, non-interlaced
Color Reduction: Selective, 256 colors
Dithering: Diffusion, 88%
Transparency: On, Matte: None
Transparency Dither: Diffusion Transparency Dither, 100%
Do any of you know of a good php class I can use to download an image from a remote source, re-size it to 120x120 and save it with a file name of my choosing?
So basically I would have an image at "http://www.site.com/image.jpg" save to my web server "/images/myChosenName.jpg" as a 120x120 pixels.
Thanks
You can try this:
<?php
$img = file_get_contents('http://www.site.com/image.jpg');
$im = imagecreatefromstring($img);
$width = imagesx($im);
$height = imagesy($im);
$newwidth = '120';
$newheight = '120';
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,'/images/myChosenName.jpg'); //save image as jpg
imagedestroy($thumb);
imagedestroy($im);
?>
More information about PHP image function : http://www.php.net/manual/en/ref.image.php
You can resize keeping the ratio of image
$im = imagecreatefromstring($img);
$width_orig = imagesx($im);
$height_orig = imagesy($im);
$width = '800';
$height = '800';
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
If you're looking to have the ability to do this for both jpg and png file formats, here's what helped me:
$imgUrl = 'http://www.example.com/image.jpg';
// or $imgUrl = 'http://www.example.com/image.png';
$fileInfo = pathinfo($imgUrl);
$img = file_get_contents($imgUrl);
$im = imagecreatefromstring($img);
$originalWidth = imagesx($im);
$originalHeight = imagesy($im);
$resizePercentage = 0.5;
$newWidth = $originalWidth * $resizePercentage;
$newHeight = $originalHeight * $resizePercentage;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
if ($fileInfo['extension'] == 'jpg') {
imagecopyresized($tmp, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagejpeg($tmp, '/img/myChosenName.jpg', -1);
}
else if ($fileInfo['extension'] == 'png') {
$background = imagecolorallocate($tmp , 0, 0, 0);
imagecolortransparent($tmp, $background);
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
imagecopyresized($tmp, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagepng($tmp, '/img/myChosenName.png');
}
else {
// This image is neither a jpg or png
}
imagedestroy($tmp);
imagedestroy($im);
The extra code on the png side of things ensures that the saved image contains any and all transparent sections.