PHP get rendered image content - php

I have a PHP script that it's purpose is to catch images from a quote image generator, that displays an image after each server request.
So the image is called like this:
<img src="gen/123456" alt="" />
But, my problem is, that the file that generates the image, is generating another image each time it's called, so after I open my browser and I see the quote, trying to access it's source gives an empty document (The current image id number has been changed, so you cannot access the image from 123456, because now there's a different number).
How can I get the first generated image which is rendered in the browser's first request, without accessing it's the actual image URL?

Related

Dynamically Resize Images with PHP and Protect them from Direct Original Size Access

Think stock images. You have a full-size original that can only be downloaded after purchase. You only want to have that image once on your server, at full-size. But you want to display that image in various places in smaller sizes (responsively reduced for mobile devices as well).
I've got various pieces of this puzzle, but have not been able to make them all work together:
With TimThumb or CImage I can resize the images server-side, and using jQuery I can dynamically change the image source based on the user's screen size.
With PHP and .htaccess I can place the image files outside of the webroot and route a spoof URL containing image name to the PHP file that will read the actual image and send a header with the image data. Fine.
But TimThumb and CImage work with the real image URLs and they test to make sure that the given URL is the actual file path of the image.
If I could send the actual image data to the image resizing script rather than the URL to the image, it should work out from there. Since the PHP script would be reading the image data, I could check to see that the user has been given the proper credentials before doing the read.
Are there any other solutions you can think of besides hacking TimThumb or CImage or writing my own custom image resizing script?
Thank you
The answer just came to me.
With .htaccess, route all images through the image processing script. On the first line of the image processing script I will include my own custom script. My custom script will check the GET parameters against the actual image to determine if the user has the credentials to be served the image being requested at the size it is being requested.
If so, let the image processing script continue, if not, exit out or change the GET parameters to that the image processing script serves a placeholder image.
VoilĂ !

Save Random Image with PHP

I need to save a random image from website to my computer. I know how to save images with PHP (I use curl), but I can't properly save random (dynamic) images.
Let's say that website (somesite.com) has some text content and IMG tag which looks like <img src='somesite.com/image.php'> and displays a random image. If I use browser, I can simply right-click and select "Save Image", so the image that I see on the screen will be saved (and they both will be the same).
However, if I use curl to open somesite.com (because I need to grab image description as well) and then use curl again to open somesite.com/image.php, images will be different (because a random one is selected every time user requests image.php).
Put simply, if somesite.com has text "this is rose", image.php will display a rose, and everything will look fine on browser. But if I use curl to open site, it may have text "this is tulip" and when I request image.php to save that image, another image will be displayed. How do I get exactly the same image saved that is displayed when first request is sent?
I guess it should be cached somewhere, or what?
It's not cached anywhere you can most likely reach. The best way to do this would be to put the randomizing into the calling file, so something like <img src='somesite.com/image.php?data=<?php echo $someRandomValue; ?>'>. You'd then use this data parameter to generate the same image every time this image is requested with that same data parameter. Of course: if there are multiple variables that affect the image, you can add more parameters to the url or simply put them in an array and pass it along base64 encoded.

How can I make an image refresh everytime it is called on a page

By default when a webpage is loaded, images are loaded one single time for each image. If you have 5 instances of the same image on a page, that image is loaded once, and then used in all 5 places seemingly from the cache of the first image load.
What I want to do is have a single image displayed 5 times on a page, and each time the images is called have it re-loaded.
The reason for this is I have an image that is called from a database and each time the image is called it loads a different picture. Now this works perfectly when refeshing the page, but not when the image is loaded multiple times on the same page.
For example if I put the following into a webpage:
<img src='http://bannerpillar.com/u/viraladmin.jpg'>
The image loads perfectly. If I reload the page, a different picture is displayed for each time the page reloads. However if I add the image to a page in 2 different locations, the same one picture is displayed from both locations.
How can I make it so the image is refreshed every time it is called on a page? Is that possible?
Try adding something to the end of the file reference, like this:
<img src='http://bannerpillar.com/u/viraladmin.jpg?<?=rand(11111,99999)?>'>
The browser will think each image is unique and load each one separately rather than using the image from cache.
You can find a tutorial on this exact question here:
http://www.marcofolio.net/webdesign/php_random_image_rotation.html
Basically, in the src, you call a php file. The php file loads a filename from a directory of images.
For your case, you would need to write some logic to offset the database return, so this becomes a mysql question as well?
If you wanted the markup to call for an image file, you could redirect using a .htaccess rewriterule
Random no. generetion can be a good option.
but more better if you load those images serially i.e. one after another
n u can use looping for that.
Add a ?rand= to the image url.
<img src='http://bannerpillar.com/u/viraladmin.jpg?rand=<?php echo rand(100000,999999); ?>'>

Displaying images in webpage from HTML

I have a script where by users upload an image on a website and the image gets uploaded to an FTPServer. The name of the file is stored in a database, so when users click on a link, the query string is used as a reference in the Database to get the image name. The only part I'm stuck on right now, is how to display the image on my webpage using php. Is there a specific function to get a copy from the FTP server and display the image? I dont want do download the image to the web server, and then display the image and then delete it, as this could take up a lot of space if the web site has many visitors.
The "big image hosts" typically have web access to their "storage servers" as well. So when a file is uploaded to their image server it's accessible with a URL. You'd want to determine the url that points to the file you just uploaded and place that in your html (in the img tag. Let the user's browser retrieve the image, as opposed to your web server first retrieving it and then displaying the page.
You have to store the image on the web server, once you do that you can display it on your web page however you like, php, ajax, javascript, your choice.

Php gd how to display image for each user without saving

I want a help,I created a facebook app using php gd.The program is when user open the app a image will appear.The image contains the username of the user,the profile pic and random generated nick name.Iam saving the output image to the server as resized.jpg and post that image to the users wall using facebook graph.
The problem is when 2 users use the app at same time,the output varies.
How to generate image to each user without saving it to the server and post to facebook.
now iam using html img tag to display the image in app..
Answer is quite simple: your image file name must have different names per user. If you have user id in $uid variable,why don't you save target file as:
imagejpeg( $rImg, "resized".$uid.".jpg" );
This way you'll have different images per user.
Rather than saving the file to your server as "resized.jpg", what you want to do is to output the generated image directly to the user. From http://php.net/manual/en/function.imagejpeg.php it says to set filename to NULL, which causes the output to be sent to the user instead of to a file. Note: you may also need to set the correct Content-Type using the header function.
This method is an alternative to Tomasz's. With his method, each image is cached on your server. With mine, the image is generated each time the page is requested. Here is an example showing the difference:
<img src="http://example.com/resized1234.jpg"> #1234 is the user's id
<img src="http://example.com/generate-image.php?uid=1234">
In your case, you said you are posting the image directly to Facebook, so I would advise my method, as Facebook will store its own copy of the image if you post it on their site, and won't need to be cached. Also, you won't have a other user's images piling up on your site.
Let me know if you need me to clarify anything in my answer!

Categories