I'm building a website which uses PHP GD to create images based on user inputted data.
So for instance I'd have something like this:
$image = imagecreate(125, 125);
$blue = imagecolorallocate($image, 0, 0, 255);
imagepng($image, "1.png");
imagedestroy($image);
header("Location: ../index.php");
To generate the image 1.png and go back to the index of the site.
On the index I'm simply displaying the code if the file exists with some more simple php
<?php if(!file_exists("php/1.png"))
echo ("<center><h2> No panels added!</h2></center>");
else
echo('<img class="p_image" src="php/1.png">');
?>
The problem I'm running into is that the browser will cache the last 1.png generated, even if the user deleted it and made a new one, and display that. Is there any way I can stop this from happening without having to rename the file every time?
Thanks in advance!
Just append a timestamp to the filename. That will essentially make it unique and prevent caching:
echo('<img class="p_image" src="php/1.png?'.time().'">');
Related
this is my first question and I'm planning to hang around in this forum. I'm very new to programming since I'm studying but I'm making great progress. So, with this in mind I'll try to be as detailed as possible.
The project I am working with is about creating a png-image using GD PHP. The script recieves data, calculates image WIDTH/HEIGHT according to these. From this data I then print out pixels on the image in different spots. Everything works great, it displays the image and saves it on the server, there's nothing wrong with that. But when I run the script, it outputs the image to the browser. I don't want that. I just want the script to process the image and save it to the server. I have searched plenty but haven't found anything about it.
Code for creating the image: I have to remove some code though, but it's not needed to answer my question. I suspect it's something with the header and imagecreatetruecolor. This is all the data I can give.
<?php
//Some calculations before
// --- START: CREATE IMAGE ---
$png = imagecreatetruecolor($WIDTH, $HEIGHT);
imagesavealpha($png, true);
$trans_colour = imagecolorallocatealpha($png, 0, 0, 0, 127);
imagefill($png, 0, 0, $trans_colour);
//Here's just a loop to print pixels
header("Content-type: image/png");
imagepng($png);
save($png); //Function used for saving
//Erase from memory
imagedestroy($png);
?>
I have 2 different scripts for handling images:
The first one is a watermark script for watermarking images on the fly:
$imgpath=$_REQUEST['filename'];
header('content-type: image/jpeg');
$watermarkfile="assets/img/logo_variations/logo_watermark_75.png";
$watermark = imagecreatefrompng($watermarkfile);
list($watermark_width,$watermark_height) = getimagesize($watermarkfile);
$image = imagecreatefromjpeg($imgpath);
$size = getimagesize($imgpath);
$dest_x = ($size[0] - $watermark_width)/2;
$dest_y = ($size[1] - $watermark_height)/2;
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
So the image URL for a watermarked image is: http://example.com/watermark.php?filename=assets/img/temp/temp_share.jpg or since I'm using mod_rewrite to "pretty up" my URL: http://example.com/watermark/assets/img/temp/temp_share.jpg.
Works like a charm and my reason for doing so like this is because this is on a modeling website where I want to display the images without watermarks but I use a jquery script to change the image source of the image gets right clicked(assuming a user is trying to save the image).
The script only changes the source of any image with a class of img-protected.
I've written it to ignore any image with watermark in the URL so that it doesn't try to change the already watermarked image which would result in a url like: http://example.com/watermark/watermark/img.jpg which would result in a broken image. The other part is written to remove http://example.com from the original source so I don't end up with http://example.com/watermark/http://example.com/img.jpg.
$('.img-protected').on('mousedown', function (event) {
if (event.which == 3) {
if(this.src.indexOf("watermark") > -1) {
return false;
}
else {
src = this.src.replace('http://example.com/','');
this.src = 'http://example.com/watermark/' + src;
}
}
});
All of this works exceptionally well until I added another image handling script:
I'm using TimThumb.php which is an on the fly image resize script I use for creating gallery icons instead of uploading an icon and a full size image(this is how I wish to keep doing so as well).
The problem I am facing is this:
If I have an image that is being turned into a thumbnail using TimThumb.php which I renamed to thumb.php on my server the URL is http://example.com/thumb.php?src=gallery/goth/industrial_brick/5361ae7de9404.jpg&w=350&h=500a=c&s=1&f=11 which gives me an icon for 5361ae7de9404.jpg.
All of my icons have a class of img-protected which means on right click the above URL is going to be changed to the watermarked one.
This is where it fails.
The outputed URL when right clicked is http://example.com/watermark/http://www.example.com/thumb.php?src=gallery/goth/industrial_brick/5361ae7de9404.jpg&w=350&h=500a=c&s=1&f=11 which results in a broken image.
I manually tried making the URL into http://example.com/watermark/thumb.php?src=gallery/goth/industrial_brick/5361ae7de9404.jpg&w=350&h=500a=c&s=1&f=11 to see if that would change anything but it still results in a broken image.
What I need is to be able to also watermark the generated icons from thumb.php using watermark.php.
Is there a way to combine these two scripts or a workaround to make this work?
I'm at a complete loss here.
EDIT: I am fully aware that advanced users can still acquire the non watermarked image since it's already been downloaded to there device, but I don't expect a high volume of users to visit this particular website as this is simply a local models portfolio.
I am using php to rotate a photo on server. When I click a button 'rotate' I use ajax to load php page, perform rotation and refresh div with new image.
Everything works and the image has been rotated with the correct dimensions but the image itself hasn't rotated. If i hit f5 to refresh page, the image is displayed corrected. Does anyone know why the image isn't displayed correctly before refreshing.
$photo_path= $result['photo_path'];
$src = imagecreatefromjpeg($photo_path);
$rotate = imagerotate($src, 90, 0);
#unlink($photo_path);
imagejpeg($rotate,$photo_path);
imagedestroy($src);
imagedestroy($rotate);
Any ideas?
try this,
$file="myimage.jpg";
$file1 = imagecreatefromjpeg($file);
$file2 = imagerotate($file1, $degrees, 0);
file_put_contents("newimage.jpg",$file2);
Maybe use JavaScript as this is a client side problem.
Update: this is very basic but test it out and see if it works;
<script>
function changeImage(o){
o.src = '//' + (new Date()).getTime(); // time to help stop caching
}
</script>
<img src="//" onclick="changeImage(this)" />
I managed to solve it. Thanks for all the help.
What i did was just give the photo a new file name and update mysql table. Must have something to do with the image being cached and I was keeping the new rotated file with the same file name.
Thanks again.
You can add a question mark and a random number after the extension of the file and this makes the user browser ignore the local cache, and reload the file. For example:
myimage.png?789 <- Random number
I'm fairly new to php but I'm trying to send an image to a buffer or some sort of temporary place where I can access later. The script I'm calling merges a bunch of images into one, then displays that image (see it in action here - change query parameters to change image). Here's a piece of my code so you have an idea of how that's happening:
$dest = imagecreatefrompng($img0);
$src12 = imagecreatefrompng($img12);
imagecolortransparent($src12, imagecolorat($src12, 0, 0));
//copy and merge
$src12_x = imagesx($src12);
$src12_y = imagesy($src12);
imagecopymerge($dest, $src12, 0, 0, 0, 0, $src12_x, $src12_y, 100);
// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
However, this is an external script so I'd like to be able to pull that image from another page. I'm not sure what the best way to do it is... temporarily store it or pass the image back through. The one constraint is that the image has to exist before the content of the parent page is loaded. How can I make this happen?
If I understand correctly what you mean;
1) You want to pull the image from another website and save it to disk, you can do this;
file_put_contents("img.png", file_get_contents("URL-TO-IMAGE"));
2) You want to just display it? As the URL aboves headers are to display an image, you can put it right into an IMG tag.
<img src="URL-TO-IMAGE">
There is a way I write eg. http://www.examle.com/hello/image.png (or in another form, it doesn't matter) and get image with text "hello" in it?
I mean, how to run some php script when loading image (create image "on the fly", no caching) and be able to use it like ordinary image so I can embed it somewhere and everytime the page with image is loaded it loads image with current numbers/stats, whatever.
Something like this banner:
http://cache.www.gametracker.com/server_info/201.4.55.227:29009/b_560_95_1.png
return this:
Can someone give me a hint how to do that?
EDIT:
I know how to create image, but issue is how to update it (recreate it) when is loaded on website. I need image actually provide "live" data.
One way would be probably using cron jobs and update image "outside" every period of time, in case the name remain same, it would do the trick, although it's not live.
You could use PHP GD: http://www.php.net/manual/en/book.image.php
Example (from http://www.php.net/manual/en/image.examples-png.php):
<?php
header("Content-type: image/png");
$string = $_GET['text'];
$im = imagecreatefrompng("images/button1.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
?>
The docs should point you in the right direction.