家好动态

网站建设就要用php高级程序做自动缩略图片生成

点击次数:  更新时间:2014-06-26 22:01:05  【打印此页】  【关闭

 

<?php
 
/*
 *        $o_photo 原图路径
 *        $d_photo 处理后图片路径
 *        $width   定义宽
 *        $height  定义高
 *        调用方法  cutphoto("test.jpg","temp.jpg",256,146);
 */
 
function cutphoto($o_photo, $d_photo, $width, $height) {
 
    $temp_img = imagecreatefromjpeg($o_photo);
    $o_width = imagesx($temp_img);                                //取得原图宽
    $o_height = imagesy($temp_img);                                //取得原图高
//判断处理方法
    if ($width > $o_width || $height > $o_height) {        //原图宽或高比规定的尺寸小,进行压缩
        $newwidth = $o_width;
        $newheight = $o_height;
 
        if ($o_width > $width) {
            $newwidth = $width;
            $newheight = $o_height * $width / $o_width;
        }
 
        if ($newheight > $height) {
            $newwidth = $newwidth * $height / $newheight;
            $newheight = $height;
        }
 
        //缩略图片
        $new_img = imagecreatetruecolor($newwidth, $newheight);
        imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);
        imagejpeg($new_img, $d_photo);
        imagedestroy($new_img);
    } else {                                                                                //原图宽与高都比规定尺寸大,进行压缩后裁剪
        if ($o_height * $width / $o_width > $height) {        //先确定width与规定相同,如果height比规定大,则ok
            $newwidth = $width;
            $newheight = $o_height * $width / $o_width;
            $x = 0;
            $y = ($newheight - $height) / 2;
        } else {                                                                        //否则确定height与规定相同,width自适应
            $newwidth = $o_width * $height / $o_height;
            $newheight = $height;
            $x = ($newwidth - $width) / 2;
            $y = 0;
        }
 
        //缩略图片
        $new_img = imagecreatetruecolor($newwidth, $newheight);
        imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);
        imagejpeg($new_img, $d_photo);
        imagedestroy($new_img);
 
        $temp_img = imagecreatefromjpeg($d_photo);
        $o_width = imagesx($temp_img);                                //取得缩略图宽
        $o_height = imagesy($temp_img);                                //取得缩略图高
        //裁剪图片
        $new_imgx = imagecreatetruecolor($width, $height);
        imagecopyresampled($new_imgx, $temp_img, 0, 0, $x, $y, $width, $height, $width, $height);
        imagejpeg($new_imgx, $d_photo);
        imagedestroy($new_imgx);
    }
}
 
cutphoto("http://tp3.sinaimg.cn/1700691210/180/1280120708/1", "temp1.jpg", 100, 100);
?>

为什么选择我们?

  今天,我们提供无数的优秀建站方案供您选择,你只需要花费几百元的开销,即可拥有价值几千甚至有的公司报价上万的网站产品。为了替中国中小型企业有效控制在网站建设的开销,我们这种工作室是有必要存在的,我们都是专业的设计人员,花三分之一的钱,甚至更少,为你打造知名一个品牌网站;我们还专门开设计好了的平面图还未写程序的,共您参考选择,缩短你宝贵的时间。我们争取做到最好的服务,所以一个月是定量的,展示型网站3个,营销型网站1个,网店商城1个。家好网络与您在互联世界共进退。

 

家好网络原创文章,本文地址:http://www.jiahaonet.com/news/news141.html,转载请注明出处。