duanhuilao0787 2014-02-24 20:24 采纳率: 100%
浏览 57
已采纳

PHP createimagefromjpeg()如何包含所有图像类型

is there a way that i can use createimagefromjpeg() to save an image from a url but allow all image file types?

It seems obvious that createimagefromjpeg only allows jpeg / jpg.

Here is my current process:

    // create the image and save it
    $imagefile = iageURL;
    $resource = imagecreatefromjpeg($imagefile);
    // X amount of quality is the last number
    imagejpeg($resource, "images/covers/imagename.jpeg", 50);
    imagedestroy($resource);

This works great because it allows me to reduce the image quality. But i get an error when trying to use anything other than a jpeg / jpg.

Is there a way around this? Or perhaps a better method of saving images (at a reduced quality) from a url.

Craig.

  • 写回答

1条回答 默认 最新

  • dry18813 2014-02-24 20:27
    关注

    You need to check the file type - the best way is to check the mime type if you can, then you can do a switch and utilize the other image creation functions in GD lib such as imagecreatefromgif() and imagecreatefrompng() etc:

    switch($mime_type) {
        case 'image/jpeg':
        case 'image/jpg':
            $img = imagecreatefromjpeg($filename);
            break;
        case 'image/png':
            $img = imagecreatefrompng($filename);
            break;
        case 'image/gif':
            $img = imagecreatefromgif($filename);
            break;
    }
    

    Another option is for you to grab the image's data into a string and use imagecreatefromstring() to create an image handle from raw data:

    $filename = 'http://yoursite.com/yourlogo.jpg';
    $img = imagecreatefromstring(file_get_contents($filename));
    

    From the manual: These types will be automatically detected if your build of PHP supports them: JPEG, PNG, GIF, WBMP, and GD2.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂