This Code saves the image like it's suppose to, but instead of displaying it as a picture, what is displayed is a line of text inside the picture. help?
<?php
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, "modified-images/".$codigo2."_modified_picture_status_".$status.".jpg");
imagedestroy($image);
imagedestroy($watermark);
?>
If you are using the second parameter of imagejpeg, the image will not be outputted to the browser but ONLY saved to the file.
Try omitting the second parameter if you don't need to save it as a file, and it should output directly to the browser.
If you want to do both, try a print(file_get_contents($imagepath)) after your current block of code. $imagepath should obviously contain the path that you wrote the image to.
make two lines:
// save image
imagejpeg($image, "modified-images/".$codigo2."_modified_picture_status_".$status.".jpg");
// output image
imagejpeg($image);
Related
Right now I am using a php script (let's say the save-page) to create a jpeg and save it to the server. Although it all works, when opening the save-page, the jpeg is also shown, which I don't really want. I just want the jpeg to be created and saved to the desired destination. To be clear, the saving is already working. The is part of the script that I am using.
header('Content-type: image/jpeg');
$jpg_image = imagecreatefromjpeg('*link to the base image*');
$white = imagecolorallocate($jpg_image, 0, 0, 0);
$font_path = 'interstate.ttf';
imagettftext($jpg_image, 14, 0, 44, 41, $white, $font_path, $text);
imagejpeg($jpg_image);
// save jpg to widget folder
imagejpeg($jpg_image, 'widget/vehicle_1_' . $id . '.jpg');
//free up memory
imagedestroy($jpg_image);
How can I change the script so that it saves the jpg to the widget folder, but not show it on this page when activated? Thanks in advance!
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.
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've been trying to get transparency to work with my application (which dynamically resizes images before storing them) and I think I've finally narrowed down the problem after much misdirection about imagealphablending and imagesavealpha. The source image is never loaded with proper transparency!
// With this line, the output image has no transparency (where it should be
// transparent, colors bleed out randomly or it's completely black, depending
// on the image)
$img = imagecreatefromstring($fileData);
// With this line, it works as expected.
$img = imagecreatefrompng($fileName);
// Blah blah blah, lots of image resize code into $img2 goes here; I finally
// tried just outputting $img instead.
header('Content-Type: image/png');
imagealphablending($img, FALSE);
imagesavealpha($img, TRUE);
imagepng($img);
imagedestroy($img);
It would be some serious architectural difficulty to load the image from a file; this code is being used with a JSON API that gets queried from an iPhone app, and it's easier in this case (and more consistent) to upload images as base64-encoded strings in the POST data. Do I absolutely need to somehow store the image as a file (just so that PHP can load it into memory again)? Is there maybe a way to create a Stream from $fileData that can be passed to imagecreatefrompng?
you can use this code :
$new = imagecreatetruecolor($width, $height);
// preserve transparency
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
imagealphablending($new, false);
imagesavealpha($new, true);
imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);
imagepng($new);
imagedestroy($new);
It will make a transparent image for you. Good Luck !
Blech, this turned out to ultimately be due to a totally separate GD call which was validating the image uploads. I forgot to add imagealphablending and imagesavealpha to THAT code, and it was creating a new image that then got passed to the resizing code. Which should probably be changed anyway. Thanks very much to goldenparrot for the excellent method of converting a string into a filename.
Do I absolutely need to somehow store the image as a file (just so that PHP can load it into memory again)?
No.
Documentation says:
You can use data:// protocol from php v5.2.0
Example:
// prints "I love PHP"
echo file_get_contents('data://text/plain;base64,SSBsb3ZlIFBIUAo=');
I'm trying to create a dynamic image generator that also caches the images.
Everything works perfectly, except that for some reason when I save an images and try to display it at the same time, it shows a broken image icon (like: http://cl.ly/image/3K1f0R0n1R0U).
Code sample:
// some string parsing junk happens up here, but removing that didn't change
// what I've been having a problem with
header("Content-type: image/png;");
if(file_exists($fullFileName)){ // serve the file if it already exists
ob_clean();
flush();
readfile($fullFileName);
exit;
} else { // create the file if it doesn't exist
$my_img = imagecreate(200, 80);
$background = imagecolorallocatealpha($my_img, 0, 0, 0, 127);
$line_colour = imagecolorallocatealpha($my_img, $color["r"], $color["g"], $color["b"], $color["a"]);
imagesetthickness($my_img, 1);
imageline($my_img, 30, 45, 165, 45, $line_colour);
//////////////////////////////////////////
// the line that's causing my troubles: //
//////////////////////////////////////////
imagepng($my_img, $fullFileName);
imagecolordeallocate($line_color);
imagecolordeallocate($background);
imagedestroy($my_img);
}
So, the images are created just fine, save just fine, and display exactly like I want when I refresh the page (because it grabs the saved file).
If I switch that darn line above to:
imagepng($my_img);
Then the image shows up fine, but it doesn't save (since I'm not telling it to).
I thought that this topic:
PHP echoing jpeg image fails, but writing same image to file works great. Why?
Sounded similar, but removing the trailing white space still didn't change what's happening. Other threads mentioned it could be a problem with other content being sent before the headers, but there is nothing like that in this file.
Any ideas on what could be going wrong? No errors are being logged, and I'm out of ideas.
Thanks!
-Zach
EDIT:
It was explained that I was destroying my image, which I missed. I ended up using this:
header("Content-type: image/png");
if(!file_exists($fullFileName)) {
$my_img = imagecreate($dashWidth + $dashSpace, $height);
$background = imagecolorallocatealpha($my_img, 0, 0, 0, 127);
$line_colour = imagecolorallocatealpha($my_img, $color["r"], $color["g"], $color["b"], $color["a"]);
imagesetthickness($my_img, 1);
imageline($my_img, 0, $height-1, $dashWidth-1, $height-1, $line_colour);
imagepng($my_img, $fullFileName);
imagecolordeallocate($my_img, $line_color);
imagecolordeallocate($my_img, $background);
}
ob_clean();
flush();
readfile($fullFileName);
exit;
you mean
header("Content-type: image/png"); // without ;
and also, you are destroying your image, read again the file
readfile($fullFileName);
because imagepng($my_img, $fullFileName); only saves it.
From the manual, imagepng
Outputs or saves a PNG image from the given image
Note the "or". Try making two calls to imagepng. One call to save it to a file and one call without the filename parameter to output the image to the browser.
Try to open image with you text editor.
Turn off error_reporting.
Show you broken image.