Save Random Image with PHP - 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.

Related

How to generate random embed image url

I'm trying to hide my image url including dir path. I tried base64 but it always shows the same url, I need a different one everytime I hit refresh. I then tried to create a temporary one using tempnam, but it only adds some characters at the end of the link and don't change the dir path.
I also tried createimagefrompng but it gives me either my website with no picture, or just a picture of a white frame.
When my player goes on the page, the page generate a random number from 1 to 5 and letter A or B, go in folder "A" or "B" and search for the picture with the generated number in that folder.
The Problem is the picture url is images/A/1.png, I want it to be random so he can't use a script to refresh until he gets A/3.
And I want it to be different everytime because he could find A/3 embed url and use the script to find that embed url.
Thank you!
Jessie
Ok managed to finally make it work!
I'm using a mix of things I've already tried.
So first the base64encode was great, but it always had the same url which I didn't want.
So I searched on how create a new encoding that I could make random and found that code: https://gist.github.com/LogIN-/e451ab0e8738138bc60b
I then generate a random key and encode the image link using that random key.
I then call an php image using createimagefrompng and put the key and encoded image link in the php url (www.example.com/image.php?encoded=jsnda9d9832rm&key=emd39023)
And then in that php decode the url using the key and show the image ^^
Hope it can helps other, also if you see something I could change so my code will be lighter or have better performance please don't hesitate :)

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); ?>'>

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!

PHP get rendered image content

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?

Save DIV content as image

I have a div that shows some content (several images, text, floating elements, etc) and I would like to add the posibility for a user to do something like "Save this content as image".
How can i do that? I read all the php docs but there's nothing good to do this, even imagegrabwindow sucks because of its high load, browser limitations, and also no X/Y or width/height control.
So basically, I want to do this:
Get #div content, click on Save as Image, then the user gets a .JPG or .PNG of that div, as if it were a printscreen (screenshot).
I don't want to pre-generate the content using GD, and neither do I want to save as .PDF.
The only thing I can really think of is to serialize the contents/placement/attributes of the content in the div, send that to the server, and have the server recreate the same elements, same positioning, same attributes with GD.
Off the top of my head, I'd see maybe inspecting the div and creating basically a form post, something like:
POST['images'][0]['file']='plane.jpg'
POST['images'][0]['position']['x']=23
POST['images'][0]['position']['y']=13
...repeat for each image
POST['text'][0]['content']='this is a plane!'
POST['text'][0]['size']='10px'
POST['text'][0]['font']='Arial'
The server could use this data to recreate what the user created in their browser.

Categories