卷曲和调整大小远程图像 [英] curl and resize remote image

查看:200
本文介绍了卷曲和调整大小远程图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此脚本下载和调整远程图像的大小。在调整大小部分出了问题。是什么?

I use this script to download and resize a remote image. In the resize part something goes wrong. What is it?

<?php
$img[]='http://i.indiafm.com/stills/celebrities/sada/thumb1.jpg';
$img[]='http://i.indiafm.com/stills/celebrities/sada/thumb5.jpg';
foreach($img as $i){
    save_image($i);
    if(getimagesize(basename($i))){
        echo '<h3 style="color: green;">Image ' . basename($i) . ' Downloaded OK</h3>';
    }else{
        echo '<h3 style="color: red;">Image ' . basename($i) . ' Download Failed</h3>';
    }
}

function save_image($img,$fullpath='basename'){
    if($fullpath=='basename'){
        $fullpath = basename($img);
    }
    $ch = curl_init ($img);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $rawdata=curl_exec($ch);
    curl_close ($ch);




    // now you make an image out of it

    $im = imagecreatefromstring($rawdata);

    $x=300;
    $y=250;

    // then you create a second image, with the desired size
    // $x and $y are the desired dimensions
    $im2 = imagecreatetruecolor($x,$y);


    imagecopyresized($im2,$im,0,0,0,0,$x,$y,imagesx($im),imagesy($im));


    imagecopyresampled($im2,$im,0,0,0,0,$x,$y,imagesx($im),imagesy($im));

    // delete the original image to save resources
    imagedestroy($im);



    if(file_exists($fullpath)){
        unlink($fullpath);
    }
    $fp = fopen($fullpath,'x');
    fwrite($fp, $im2);
    fclose($fp);

    // remember to free resources
imagedestroy($im2);



}
?>


推荐答案

当我运行它,PHP给我以下错误:

When I run it, PHP gives me the following error:


警告:fwrite()期望参数2是字符串,第53行上的资源...

Warning: fwrite() expects parameter 2 to be string, resource given ... on line 53

fwrite() 将字符串写入文件。您要使用GD函数 imagejpeg() 保存GD资源到文件。它适用于我,当我更改

fwrite() writes a string to a file. You want to use the GD function imagejpeg() to save the GD resource to a file. It works for me when I change

$fp = fopen($fullpath,'x');
fwrite($fp, $im2);
fclose($fp);

imagejpeg($im2, $fullpath);






在无关的备注上使用cURL抓取文件,您只需使用 file_get_contents() 而不是cURL,假设PHP被配置为允许fopen函数中的完整URL。 (我相信这是默认情况下。)有关详细信息,请参阅 file_get_contents()手册页上的备注部分。函数二进制安全的,因此除了文本文件外,它还适用于图像。要使用它,我只是用这一行替换所有六行cURL函数:


On an unrelated note, if all you're doing with cURL is grabbing the files, you can simply use file_get_contents() instead of cURL, assuming PHP is configured to allow full URLs in fopen functions. (I believe it is by default.) See the Notes section on the file_get_contents() manual page for more info. The function is binary-safe, so it works with images in addition to text files. To use it, I just replaced all six lines of the cURL functions with this line:

$rawdata = file_get_contents($img);






更新:

您可以在数组键中为它们指定一个新的文件名,如下所示:


Update:
In response to your question below, you can specify a new file name for them in the array keys like so:

<?php
$img['img1.jpg']='http://i.indiafm.com/stills/celebrities/sada/thumb1.jpg';
$img['img2.jpg']='http://i.indiafm.com/stills/celebrities/sada/thumb5.jpg';
foreach($img as $newname => $i){
    save_image($i, $newname);
    if(getimagesize(basename($newname))){

这篇关于卷曲和调整大小远程图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆