简体   繁体   English

将网址中的任何图像类型另存为jpg

[英]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)? 是否可以将具有任何类型的图像(可以是jpg,png,gif等)的url作为jpg保存为php中的文件夹,而无需执行多个步骤(检查扩展名,保存为相同类型并转换为jpg)?

sample urls 样本网址

  1. http://jdownloader.org/_media/knowledge/wiki/jdownloader.png?cache=&w=512&h=512 (.PNG) 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) http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com(.jpg

The url many not always contain imagename.ext kind of structure. 许多URL并不总是包含imagename.ext类型的结构。

for a code like this (from here PHP save image file ) 对于这样的代码(从这里PHP保存图像文件

$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. 我阅读了从URL创建任何文件类型的 图像,从PHP URL保存图像等等,但是不确定如何获得一个简单的解决方案。

Thanks for any help. 谢谢你的帮助。

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 imagecreatefromjpeg函数用于从文件或URL创建图像

Also allow_url_fopen setting must be set to On in php.ini file for creating image from URL 此外,必须在php.ini文件中将allow_url_fopen设置设置为On才能从URL创建图像

For more details please refer the documentation mentioned in below url 有关更多详细信息,请参阅下面网址中提到的文档

http://php.net/manual/en/filesystem.configuration.php http://php.net/manual/zh/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);

 }

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) 另外,对于您尝试过的操作-仅将文件扩展名重命名为.jpg并不会使它实际上是jpg(例如,IrfanView在打开此类文件时会警告您,并询问您是否要将扩展名改回来以更正该扩展名)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM