Home > Backend Development > PHP Tutorial > 如何在下载图片的PHP里添加上自动缩略图

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

WBOY
Release: 2016-06-06 20:49:16
Original
969 people have browsed it

这段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>
Copy after login
Copy after login

回复内容:

这段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>
Copy after login
Copy after login

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

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template