I want to create image from php using the function imagecreatefromjpeg(string $filename ),
but when I am providing a image URL as a parameter to this function then this function is not able to create image.
$pic = imagecreatefromjpeg('http://www.example.com/image.jpg');
header("Content-type: image/jpeg");
imagejpeg($pic);
imagedestroy($pic);
You can actually create images from remote files, but please be sure the 'fopen wrappers' have been enabled, see also http://php.net/manual/en/function.file.php
if it doesn't work, what kind of error do you see? and what kind of variable is $pic?
I can't check this right now, but i'd wager it has to be a local file. I.e. you need to have the file on your server.
php.net says: A URL can be used as a filename with this function if the fopen wrappers have been enabled. (http://php.net/manual/en/function.imagecreatefromjpeg.php)
Related
I am trying to migrate some content from one resources into another and need to save some images (several hundred) located at a remote resource.
Suppose I have only the URL to an image:
https://www.example.com/some_image.jpg
And I would like to save it into the filesystem using PHP.
If I were uploading the image, I essentially would do the following:
<input type="file" name="my_image" />
move_uploaded_file($_FILES['my_image']['tmp_name'], '/my_img_directory');
But since I only have the URL, I would imagine something like:
$img = 'https://www.example.com/some_image.jpg';
$file = readfile($img);
move_uploaded_file($file, '/my_img_directory');
Which of course wouldnt work since move_uploaded_file() doesn't take an output buffer as a first argument.
Essentially, I would need to get $img into the $_FILES[] array under this approach. Or may some other approach?
You can use PHP's copy function to copy remote files to a location on your server:
copy("https://example.com/some_image.jpg", "/path/to/file.jpg");
http://php.net/manual/en/function.copy.php
$image = file_get_contents('http://www.url.com/image.jpg');
file_put_contents('/images/image.jpg', $image); //Where to save the image on your server
I thought of using getimagesize($url); but there are still many cases where i can access the image through the browser but the same image returns nothing from getimagesize($url);
$url = 'http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 06826 05102 04 0026 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large] ';
Just check the Content-Type header for the string image.
Just use the function get_headers(): http://php.net/manual/en/function.get-headers.php
You can also use curl if it's available on your system, details here: Get mime type of external file using cURL and php
To use getimagesize() you need to download the url and save it as a local file. Then pass the string of the filename stored locally to getimagesize().
What are some possible ways to save an image or make use of it that is generated from a PHP script. Using save as it does not help though.
This is not an image created by me that's why I want to avoid get_contents.
here is the picture
and here is the url
https://render01.fontshop.com/fonts/font_rend.php?idt=f&id=38005&rbe=fsifr&rt=how+do+I+save+this?&rs=38&w=500&bg=ffffff&fg=000000&tp=0.0
Just write the content of the URL to a file
<?php
file_put_contents("img.png", file_get_contents("http://render01.fontshop.com/fonts/font_rend.php?idt=f&id=38005&rbe=fsifr&rt=how+do+I+save+this?&rs=38&w=500&bg=ffffff&fg=000000&tp=0.0"));
Using file_put_contents() function. If you don't have data in variable and want to readout use file_get_contents()
Since you are not generating the image in your own code, the simplest would be a combo of file_get_contents and file_put_contents:
$url = '...'; // your url here
$data = file_get_conents($url);
file_put_conents('image.png', $data);
In this specific case the render is a PNG image, but if there's a possibility of it being a JPEG or something else then you need to somehow detect that as well. I 'm not giving any suggestions for this because there's not enough info to go by.
You can define a filename in imgpng() or the other functions to tell PHP to store the picture instead of sending it to the calling browser.
I understand you want to save it on the client, with a browser, not on the server.
"Save as" worked fine for me (Firefox 7). In Chrome you'll have to specify the extension of the filename manually. Did not test other browsers, but it should work similarly
You can do this from the terminal using the curl command.
curl -o out.png 'http://render01.fontshop.com/fonts/font_rend.php?idt=f&id=38005&rbe=fsifr&rt=how+do+I+save+this?&rs=38&w=500&bg=ffffff&fg=000000&tp=0.0'
This will save the file as out.png
use imagepng function.
It will return file to browser or save it specified location.
Need to set parameter for function to save image on specified location.
For an image file (JPEG) that has been uploaded to the server via a PHP script (to a directory such as http://www.somedomain.com/images, is it a good idea to allow the client to get the image's direct address (such as http://www.somedomain.com/images/someimage.jpg and paste it into a WYWSIWYG text editor (such as TinyMCE)?
I am wondering if there is a preferable method where the direct address is encrypted?
Please, if I should just link directly to the image, just say so.
Thanks!
Note: I have modified this question from my original. Please see revisions if you are curious, but I think I was asking the question incorrectly. My apologies to the people who already answered.
As long as you check correctly WHAT is being uploaded, it shouldn't be a problem. So please at least use getimagesize or a similar function to make sure it's an image that's being uploaded, AND make sure the extension on the file is correct so that it will never be run through the PHP interpreter - to prevent someone from uploading an image with a PHP script attached.
BTW Here's a nice whitepaper on uploads and security : http://www.scanit.be/uploads/php-file-upload.pdf
Depending on the CPU Constraints of your web-hosting service you can write a service to 'serve' the images to your users.
Here is some very BASIC code, it needs spiffing up and cleaning up for XSS/etc...
<?php
$basePath = "/path/to/my/image/store/not/web/accessible/";
$file = NULL;
if (isset($_GET['file']))
$file = $_GET['file'];
if ($file != NULL)
{
$path = $basePath . $file;
// $file needs to be checked for people
// trying to hack you, but for the sake of simplicity
// i've left it out
$mime = mime_content_type($path);
$size = filesize($path);
header("Content-Length: " . $size);
header("Content-Type: " . $mime);
header('Expires: 0');
readfile($path); // Outputs the file to the output buffer
}
?>
Obviously you can put whatever security checks in here you want. But this way your files are below the web dir, and you can apply logic to thier accesibility. This is typically used more for FILE vs. Images, but you can do the same thing here.
Images Accessed like this
http://www.mysite.com/image.php?file=hello.jpg
And you can use mod_rewrite to rewrite urls like this:
`http://www.mysite.com/images/hello.jpg
Into the first url.
I Cannot stress enough the need for further security checking in the above example, it was intended to show you how to serve a file to the user using PHP. Please don't copy & use this verbatim.
Wordpress uses direct links for images. The permalink function simply puts the image on a page along with metadata for comments, but the images' SRC attributes still link directly to the image.
why are you concerned about revealing your image location. Hotlinking?
if so you can prevent hotlinking with htaccess
http://altlab.com/htaccess_tutorial.html
Didn't you get your answer already?
Every site reveals image location to the browser. It's just the way web works.
Got any reason to "encrypt" original location?
I am trying to save PhpThumb output. As what I could find on-line was not sufficient or too complex, I would like to ask if any one knows how to it?
$thumb_src="\"phpThumb/phpThumb.php?src=../apartmentsPhotos/".$num['ref']."/1.JPG&h=119&q=100\"";
echo" '<'img src=".$thumb_src />";
So what I want to do is to save the img src into an Image.
(So far I was creating the thumbnails on the fly but it seems that google and my web server donĀ“t like it too much. Saving the thumbnails will ensure that in no time I will have all my thumbnails in real files and then I will use this function just for new content.)
From phpThumb's FAQ
The best way is to call phpThumb as an object and call RenderToFile() to save the
thumbnail to whatever filename you want. See /demo/phpThumb.demo.object.php for an example. The other way is to use the 'file' parameter (see /docs/phpthumb.readme.txt) but this parameter is deprecated and does not work in phpThumb v1.7.5 and newer.
Once you have generated the URL with this line you posted:
$thumb_src="\"phpThumb/phpThumb.php?src=../apartmentsPhotos/".$num['ref']."/1.JPG&h=119&q=100\"";
Pass it as a $_GET variable to another page, call it serveThumb.php:
if (!isset($_GET['img']))
exit;
header('Content-type: application/pdf');
echo file_get_contents($_GET['img']);
You might have to add your own validation to serveThumb.php. Now you can save the result of serveThumb.php as a JPG.
Alternatively, save the contents of the image as a JPG file.
if (!isset($_GET['img']))
exit;
$img = file_get_contents($_GET['img']);
file_put_contents("myImage.jpg", $img);