SimpleImage等比缩放基本用法
使用方法示例:设定宽度,进行等比例缩放。复制代码代码如下:
使用例子:复制代码代码如下:
$url="http://f3.v.veimg.cn/meadincms/1/2013/0703/20130703100937552.jpg"; $picfile = down($url);//下载图片(下载图片的路径可以处理完成后清空,这里未进行处理) $res = new SimpleImage();//图片处理实例 $res = $res->load($picfile); $tmpfile = tempfile().'.jpg';//创建一个路径文件用来保存图片 $width = '30';//设定图片的宽度 $res->resizeToWidth($width); $res->save($tmpfile);//把处理后的图片保存(无.jpg后缀) //这里总共产生了3个文件,一个是下载的图片文件,一个是临时文件,最后一个是处理的图片文件。需要优化清理掉前两个文件。
function down($url) { $http = array(); $header = "http://f3.v.veimg.cn"; if ($header) { $http['header'] = $header; }
$http['timeout'] = 50;
$ctx = stream_context_create(array( 'http' => $http, )); $content = @file_get_contents($url, 0, $ctx); sleep(1);
if (!$content) { return false; }
$tmpfile = tempfile();
file_put_contents($tmpfile, $content);
return $tmpfile; }
function tempfile() { $path = dirname(__FILE__); $path .= '/spider/' . date('Ymd') . '/'.date('His').'-' . (int)(time() / 300);
if (!file_exists($path)) { mkdir($path, 0777, true); }
do { $file = $path . '/' . dechex(mt_rand()); } while (file_exists($file));
touch($file);
return $file; }
图片处理类源码(官网地址:http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/):复制代码代码如下: