Passing value on img src $_GET issue - php

I am passing values from the img src like this:
PageA:
<img src="watermark.php?fname=<?php echo $image; ?>" />
then I ge the value on pageB:
$fname = $_GET['fname'];
$watermark_img = $passed_fname;
...
For some reason no image is showing ... I'm I missing something here?
Here is pageB full code:
<?php
$fname = $_GET['fname'];
$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
$watermark_img = $fname;
$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);
?>

Does $image contains some special characters?
Try to use PHP urlencode() function:
<img src="watermark.php?fname=<?php echo urlencode($image); ?>/>
And the, on the second page:
$fname = urldecode($_REQUEST['fname']);

If the value you pass on $fname is just the name, you might need to specify the full path.
$watermark_img = '/PATH_TO_THE_IMAGE/'.$fname;

Try the watermark PHP directly from the address bar, then view source. If there is any output before you set the jpeg header, the browser will be unable to deal with it. You might have something like
Error line 3443234
JPEGIMAGECODE
That first line makes it an invalid image.

Related

How to add watermark on image before force download

I want to add force download functionality with the help of
$this->load->helper('download');
$photo_path = "uploads/default/photos/".$photo;
$name = $photo_name.'.jpg';
$data = file_get_contents($photo_path); // Read the file's contents
$name = $photo_name.'.jpg';
force_download($name, $data);
Now i want to add watermark image over the image before downloading the image.is this possible with image manipulation library or should i try to add watermark when uploading files.
Here's how you can add the watermark before sending it to force_download:
// Get the watermark from a file
$watermark = imagecreatefrompng('watermark.png');
$wmsize = getimagesize('watermark.png');
// Get your source image
$image = imagecreatefromjpeg($photo_path);
$size = getimagesize($photo_path);
// Set the watermark to be centered within the size of the destination image
$dest_x = ($size[0] - $wmsize[0]) / 2;
$dest_y = ($size[1] - $wmsize[1]) / 2;
// Copy the watermark over the original image
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $wmsize[0], $wmsize[1]);
// Use output buffering to capture the output to send to force_download
ob_start(); //Stdout --> buffer
imagejpeg($image);
$img2 = ob_get_contents(); //store stdout in $img2
ob_end_clean(); //clear buffer
imagedestroy($image);
force_download($name, $img2);

Photo color loss after watermarking

Having a few teething problems watermarking a photo. It all works fine apart from the watermarked photo's colors become duller than they should be - very noticeable in-fact.
I'm using imagecopyresized to do my watermarking, as this specifically allows me to use PNG-24 watermarks, the others do not. I know the colors are usually OK, as I have just used readfile($url) as a test, and the photos are perfect.
Here is my script:
<?php
// get parent and watermark images & sizes
$image = imagecreatefromjpeg($url);
$imageSize = getimagesize($url);
$watermark = imagecreatefrompng('watermark.png');
$watermark_o_width = imagesx($watermark);
$watermark_o_height = imagesy($watermark);
// calculate new watermark width and position
if ($imageSize[0] > $imageSize[1] || $imageSize[0] == $imageSize[1]) {
$leftPercent = 23;
} else {
$leftPercent = 7;
}
$leftPixels = ($imageSize[0]/100)*$leftPercent;
$newWatermarkWidth = $imageSize[0]-$leftPixels;
$newWatermarkHeight = $watermark_o_height * ($newWatermarkWidth / $watermark_o_width);
// place watermark on parent image, centered and scaled
imagecopyresized(
$image,
$watermark,
$imageSize[0]/2 - $newWatermarkWidth/2,
$imageSize[1]/2 - $newWatermarkHeight/2,
0,
0,
$newWatermarkWidth,
$newWatermarkHeight,
imagesx($watermark),
imagesy($watermark)
);
// print
imagejpeg($image);
// destroy
imagedestroy($image);
imagedestroy($watermark);
?>
How can I stop this from happening? I'm reading about imagecreatetruecolor, does that solve the issue? I'm Googling "imagecreatetruecolor color loss photos" and variations but nobody really talks about this issue. If I do need this function, where would I add that to this script?
This has totally thrown a spanner in the works for me and would love for somebody to tell me where to stick it (not literally).
Here is an example of the color loss. The preview image should be exactly the same colors as the thumbnail. The thumbnails are created using readfile() whereas the previews are created using imagecreatefromjpeg and imagecopresized.
This example code works fine, by using the same characteristics as your images:
Original JPG: dark background; beautiful girl; red dress.
Watermark PNG: transparent background; text; gray color.
<?php
// Path the the requested file (clean up the value if needed)
$path = $url;
// Load image
$image = imagecreatefromjpeg($path);
$w = imagesx($image);
$h = imagesy($image);
// Load watermark
$watermark = imagecreatefrompng('watermark.png');
$ww = imagesx($watermark);
$wh = imagesy($watermark);
// Merge watermark upon the original image (center center)
imagecopy($image, $watermark, (($w/2)-($ww/2)), (($h/2)-($wh/2)), 0, 0, $ww, $wh);
// Output the image to the browser
header('Content-type: image/jpeg');
imagejpeg($image);
// destroy both images
imagedestroy($image);
imagedestroy($watermark);
// kill script
exit();
?>
Left: Output Image | Right: Original Image
Note:
The output image was compressed several times until: Original -> PHP Output -> GIMP -> Here.
After much testing, I came to the conclusion that PHP's GD Image does not support color profiles on the images that are being watermarked. I am now using Imagick and the colors are perfect.

Add text watermark to image watermark code

I am using an image watermark code that works great but I also need to add a text watermark to it.
Here is the code in full:
<?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);
?>
You can use imagestring() there are other image text function so checkout the manual for GD
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);

Dynamic Watermark in PHP

I'm using this script to create a watermark on one of client's websites the problem is that one watermark is suitable for one image and unsuitable for another.
Portrait
Landscape
Here is the script:
<?php
// this script creates a watermarked image from an image file - can be a .jpg .gif or .png file
// where watermark.gif is a mostly transparent gif image with the watermark - goes in the same directory asthis script
// where this script is named watermark.php
// call this script with an image tag
// <img src="watermark.php?path=imagepath"> where path is a relative path such as subdirectory/image.jpg
$imagesource = $_GET['path'];
$filetype = substr($imagesource,strlen($imagesource)-4,4);
$filetype = strtolower($filetype);
if($filetype == ".gif") $image = #imagecreatefromgif($imagesource);
if($filetype == ".jpg") $image = #imagecreatefromjpeg($imagesource);
if($filetype == ".png") $image = #imagecreatefrompng($imagesource);
if (!$image) die();
//This bit is the dynamic bit
if(imagesx($image) <=1100){
$watermark = #imagecreatefromgif('watermark_port.gif');
}elseif(imagesx($image) <=1600 && $imagewidth >1100){
$watermark = #imagecreatefromgif('watermark_lans.gif');
}elseif(imagesx($image) >1600){
$watermark = #imagecreatefromgif('watermark_lans.gif');
};
//End of dynamic code
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth,
$watermarkheight);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
However the dynamic watermark doesn't work. What i want is if the image is under 1100px wide then it uses the portrait version and if it is over that to use the landscape version instead.
Any idea would be greatly apprieciated.
Thanks,
David
What your "dynamic part" is doing is basically summed up as:
if something
do A
else if something
do B
else
do B
The middle one is completely redundant.
What you need is just:
$watermark =
imagecreatefromgif("watermark_".(imagesx($image) <= 1100 ? "port" : "lans").".gif");

CONVERT HEADER INTO <img>

I'd like to show the image in this page using an <img> tag instead of an header ( header("content-type: image/jpeg"); ). any ideas?
<?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 = 50; // 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);
?>
This script generates an image, its output is an image, so you can use it in a tag, but in an other page:
<img src="image.php" /> <!-- (if image.php is the name of the script you posted) -->
<img src="yourscript.php"/>
or have your script output the image in some file and then use this file.
any ideas?
Don't do it. It is theoretically possible to embed images in HTML using data: URI's, but it has too many downsides and is usually a bad idea. The classical approach using an <img> tag that references the PHP script is the way to go.
you work with the ob_start() and ob_get_contents()
if($_GET['image'])
{
ob_start();
$main_img = "a.jpg"; // main big photo / picture
$watermark_img = "b.gif"; // use GIF or PNG, JPEG has no tranparency support
$padding = 3; // distance to border in pixels for watermark image
$opacity = 50; // 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);
$s = ob_get_contents();
ob_clean();
echo $s;
}
?>
[img src="echo '?image=1'?>"]
at the end you can in the src of image tage do this
echo '?image=1'

Categories