Home > Article > Backend Development > 如何在下载图片的PHP里添加上自动缩略图
这段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类库好使。