Frage

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.

War es hilfreich?

Lösung

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)

Andere Tipps

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

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

 }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top