>백엔드 개발 >PHP 튜토리얼 >如何在下载图片的PHP里添加上自动缩略图

如何在下载图片的PHP里添加上自动缩略图

WBOY
WBOY원래의
2016-06-06 20:49:16974검색

这段PHP本来是把titlepic里的外链图片自动下载,我想让它在下载时自动缩略成固定大小的图片。求教应该怎么修改呢?

<code>    $sqltext="SELECT id,titlepic FROM {$dbtbpre}_article where titlepic like 'http://%' order by id limit 1";
    $r=$empire->fetch1($sqltext);
    if($r)
    {
        $path=ECMS_PATH."uploads\\pic\\";
        $yname=$path.basename($r[titlepic]);
        $filename='/uploads/pic/'.basename($r[titlepic]);

        if(file_exists($yname)&&filesize($yname)>0)
        {
            echo($yname."已存在<br>");  //文件存在 
            //exit('s:'.filesize($yname));
        }
        else
        {
            echo($yname."<br>down:".$r[titlepic]);

            $string=ReadFiletext($r[titlepic]);
            WriteFiletext_n($yname,$string);
            sleep(1);           
        }
        $sqltext="update {$dbtbpre}_article set titlepic='$filename' where id='$r[id]'";
        echo('<br>'.$sqltext);
        $empire->query($sqltext);
        sleep(1);   
        echo"<meta http-equiv='\"refresh\"' content='\"1;url=?act\"'>";
    }
    else
    {
        //echo"<meta http-equiv='\"refresh\"' content='\"1;url=ListAllInfo.php\"'>";
        echo"完成";
    }
    db_close();
    $empire=null;
</code>

回复内容:

这段PHP本来是把titlepic里的外链图片自动下载,我想让它在下载时自动缩略成固定大小的图片。求教应该怎么修改呢?

<code>    $sqltext="SELECT id,titlepic FROM {$dbtbpre}_article where titlepic like 'http://%' order by id limit 1";
    $r=$empire->fetch1($sqltext);
    if($r)
    {
        $path=ECMS_PATH."uploads\\pic\\";
        $yname=$path.basename($r[titlepic]);
        $filename='/uploads/pic/'.basename($r[titlepic]);

        if(file_exists($yname)&&filesize($yname)>0)
        {
            echo($yname."已存在<br>");  //文件存在 
            //exit('s:'.filesize($yname));
        }
        else
        {
            echo($yname."<br>down:".$r[titlepic]);

            $string=ReadFiletext($r[titlepic]);
            WriteFiletext_n($yname,$string);
            sleep(1);           
        }
        $sqltext="update {$dbtbpre}_article set titlepic='$filename' where id='$r[id]'";
        echo('<br>'.$sqltext);
        $empire->query($sqltext);
        sleep(1);   
        echo"<meta http-equiv='\"refresh\"' content='\"1;url=?act\"'>";
    }
    else
    {
        //echo"<meta http-equiv='\"refresh\"' content='\"1;url=ListAllInfo.php\"'>";
        echo"完成";
    }
    db_close();
    $empire=null;
</code>

这不是帝国CMS么,你的需求应该是把远程图片保存下来的同时自动等比例裁剪,那就写个函数加到WriteFiletext_n里面就行了。至于怎么裁剪图片谷歌一下,可以直接用imagecopyresampled,或者安装了ImageMagick更好,这个比GD类库好使。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.