0

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.

Community
  • 1
  • 1
aVC
  • 2,254
  • 2
  • 24
  • 46

3 Answers3

0

As a solution to your problem please refer the code snippet mentioned below

<?php
  $im=imagecreatefromjpeg('http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com');
    header('Content-type:image/jpeg');
    imagejpeg($im);
 ?>

imagecreatefromjpeg function is used for creating an image from file or URL

Also allow_url_fopen setting must be set to On in php.ini file for creating image from URL

For more details please refer the documentation mentioned in below url

http://php.net/manual/en/filesystem.configuration.php

Rubin Porwal
  • 3,736
  • 1
  • 23
  • 26
  • it is not always from jpg. it can be any file type. and thats what I was looking for. something like imagejpeg('$url') where url can be jpg, png or anything – aVC Oct 31 '12 at 06:40
  • Please try code snippet mentioned into my latest answer .It will save image independent of any file extension. – Rubin Porwal Oct 31 '12 at 07:32
0

As a solution to your problem please refer the code snippet mentioned below

       $h=get_headers('http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com',1);
       $img_content=file_get_contents('http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com');
       $extension='jpg';
      if(!empty($img_content))
      {
    switch($h['Content-type'])
    {
    case 'image/jpeg':
        $extension='jpg';
        break;
    case 'image/gif':
        $extension='gif';
        break;
    case 'image/png':
        $extension='png';
        break;      
    }

file_put_contents('abc.'.$extension,$img_content);

 }
Rubin Porwal
  • 3,736
  • 1
  • 23
  • 26
  • 1
    There is an EDIT button. Also i guess he wants to convert the image to JPG, not just change the extension. – Vultour Oct 31 '12 at 07:33
  • Yeah, not just a change of extension. But from what @Seth says, and my not being able to find anything discussed anywhere, I guess I should go the routine way: check header, and use individual functions such as createimagefromjpeg/png/gif, etc. – aVC Oct 31 '12 at 14:24
0

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)

Vultour
  • 373
  • 4
  • 15
  • I just tried fiddling with the extension thinking may be imagejpeg() would do it. I saw something about imajik, but I dont have it installed. Was hoping php can do it, before I keep adding packages. – aVC Oct 31 '12 at 14:27