Search code examples
phpimagesave-as

save any image type from a url as jpg


Is it possible to save a url with an image of any type ( it can be jpg, png, gif, etc) as jpg into a folder in php without multiple steps (checking extension, save as same type and convert to jpg)?

sample urls

  1. http://jdownloader.org/_media/knowledge/wiki/jdownloader.png?cache=&w=512&h=512 (.PNG)
  2. http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com (.jpg)

The url many not always contain imagename.ext kind of structure.

for a code like this (from here PHP save image file)

$input = 'http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com';
$output = 'google.com.jpg';
file_put_contents($output, file_get_contents($url)); 

Is it possible to save any image file type without checking the extension? I tried

$input = 'URL_string';
$output = 'path/to/folder/name'; // no extensions
file_put_contents($output, file_get_contents($url)); 
imagejpeg($output,"temp.jpg");

Dint work. I read Create Image From Url Any File Type, Saving image from PHP URL, and more, but not sure how to get a simple solution.

Thanks for any help.


Solution

  • I guess this is not possible (taking in mind all of the existing image filetypes out there) but for the most basic ones there should be libraries for converting.

    Also for the thing you tried - just renaming the file extension to .jpg doesn't actually make it a jpg (IrfanView, for example, warns you upon opening such file and asks you if you want to change the extension back to correct one)