设为首页 收藏本站
查看: 419|回复: 0

[经验分享] php 截取固定图片大小

[复制链接]

尚未签到

发表于 2017-3-27 07:04:55 | 显示全部楼层 |阅读模式
1.创建iamge处理类
<?php
include_once 'ImageResize.class.php';
date_default_timezone_set('PRC');
class Image {   
  
    protected $nameinfo;   
    protected $InputImageFileExtension;   
  
    public static function getInstance() {   
        static $instance;   
        if (!isset ($instance)) {   
            $class = __CLASS__;   
            $instance = new $class ();   
        }   
  
        return $instance;   
    }   
  
    function uploadresize($fileparam, $imageparam) {   
  $newW = $imageparam['imageW'];
  $newH = $imageparam['imageH'];
        $this->nameinfo = explode('.',$fileparam['name']);   
  
        //取得图片格式   
        $this->InputImageFileExtension = $this->nameinfo[1];   
  
        //判断是不是给了新的文件名   
        if(empty($imageparam['imagename'])){   
            $outputFileName = $this->nameinfo[0];   
        }else{   
            $outputFileName = $imageparam['imagename'];   
        }   
  
        //判断路径是否正确   
        if (!file_exists($imageparam['imagepath'])) {   
            if(!mkdir($imageparam['imagepath'])){   
                throw new Exception('image path is wrong');   
                exit;   
            }   
        }   
  
       $file_src = $imageparam['imagepath']."/". $outputFileName . "_." . $this->InputImageFileExtension;   
       //  temporary safe image storage   
       if(file_exists($file_src)){   
        unlink($file_src);   
       }   
  
       move_uploaded_file($fileparam['tmp_name'], $file_src);   
       // set image size
   $imageResize = new ImageResize();
   $imageResize->my_image_resize($file_src,$file_src,$newW,$newH,$this->InputImageFileExtension);  
    }   
}   
?>  

2.设置图片大小处理类:
<?php
class ImageResize {
/*
* 说明:函数功能是把一个图像裁剪为任意大小的图像,图像不变形
* 参数说明:输入 需要处理图片的 文件名,生成新图片的保存文件名,生成新图片的宽,生成新图片的高
* written by smallchicken
* time 2008-12-18
*/
// 获得任意大小图像,不足地方拉伸,不产生变形,不留下空白
function my_image_resize($src_file, $dst_file, $new_width, $new_height, $type) {
if ($new_width < 1 || $new_height < 1) {
echo "params width or height error !";
exit ();
}
if (! file_exists ( $src_file )) {
echo $src_file . " is not exists !";
exit ();
}
// 图像类型
$support_type = array ("jpg", "png", "gif" );

if (! in_array ( $type, $support_type, true )) {
echo "this type of image does not support! only support jpg , gif or png";
exit ();
}
//Load image
switch ($type) {
case "jpg" :
$src_img = imagecreatefromjpeg ( $src_file );
break;
case "png" :
$src_img = imagecreatefrompng ( $src_file );
break;
case "gif" :
$src_img = imagecreatefromgif ( $src_file );
break;
default :
echo "Load image error!";
exit ();
}
$w = imagesx ( $src_img );
$h = imagesy ( $src_img );
////
if($w < $new_width && $h < $new_height){
// 定义一个中间的临时图像,该图像的宽高比 正好满足目标要求
$inter_w = $w;
$inter_h = $h;
$inter_img = imagecreatetruecolor ( $inter_w, $inter_h );
imagecopy ( $inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h );
// 生成一个以最大边长度为大小的是目标图像$ratio比例的临时图像
// 定义一个新的图像
$new_img = imagecreatetruecolor ( $inter_w, $inter_h );
imagecopyresampled ( $new_img, $inter_img, 0, 0, 0, 0, $inter_w, $inter_w, $inter_h, $inter_h );
switch ($type) {
case "jpg" :
imagejpeg ( $new_img, $dst_file, 100 ); // 存储图像
break;
case "png" :
imagepng ( $new_img, $dst_file, 100 );
break;
case "gif" :
imagegif ( $new_img, $dst_file, 100 );
break;
default :
break;
}
die();

}
////
$ratio_w = 1.0 * $new_width / $w;
$ratio_h = 1.0 * $new_height / $h;
$ratio = 1.0;
// 生成的图像的高宽比原来的都小,或都大 ,原则是 取大比例放大,取大比例缩小(缩小的比例就比较小了)
if (($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1)) {
if ($ratio_w < $ratio_h) {
$ratio = $ratio_h; // 情况一,宽度的比例比高度方向的小,按照高度的比例标准来裁剪或放大
} else {
$ratio = $ratio_w;
}
// 定义一个中间的临时图像,该图像的宽高比 正好满足目标要求
$inter_w = ( int ) ($new_width / $ratio);
$inter_h = ( int ) ($new_height / $ratio);
$inter_img = imagecreatetruecolor ( $inter_w, $inter_h );
imagecopy ( $inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h );
// 生成一个以最大边长度为大小的是目标图像$ratio比例的临时图像
// 定义一个新的图像
$new_img = imagecreatetruecolor ( $new_width, $new_height );
imagecopyresampled ( $new_img, $inter_img, 0, 0, 0, 0, $new_width, $new_height, $inter_w, $inter_h );
switch ($type) {
case "jpg" :
imagejpeg ( $new_img, $dst_file, 100 ); // 存储图像
break;
case "png" :
imagepng ( $new_img, $dst_file, 100 );
break;
case "gif" :
imagegif ( $new_img, $dst_file, 100 );
break;
default :
break;
}
} else {
// end if 1
// 2 目标图像 的一个边大于原图,一个边小于原图 ,先放大平普图像,然后裁剪
// =if( ($ratio_w < 1 && $ratio_h > 1) || ($ratio_w >1 && $ratio_h <1) )
$ratio = $ratio_h > $ratio_w ? $ratio_h : $ratio_w; //取比例大的那个值
// 定义一个中间的大图像,该图像的高或宽和目标图像相等,然后对原图放大
$inter_w = ( int ) ($w * $ratio);
$inter_h = ( int ) ($h * $ratio);
$inter_img = imagecreatetruecolor ( $inter_w, $inter_h );
//将原图缩放比例后裁剪
imagecopyresampled ( $inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h, $w, $h );
// 定义一个新的图像
$new_img = imagecreatetruecolor ( $new_width, $new_height );
imagecopy ( $new_img, $inter_img, 0, 0, 0, 0, $new_width, $new_height );
switch ($type) {
case "jpg" :
imagejpeg ( $new_img, $dst_file, 100 ); // 存储图像
break;
case "png" :
imagepng ( $new_img, $dst_file, 100 );
break;
case "gif" :
imagegif ( $new_img, $dst_file, 100 );
break;
default :
break;
}
} // if3
} // end function
}
?>

3.图片固定大小及路径设置

<?php  
include "../../tools/Image/Image.class.php";  
$array = $_FILES['photo'];

//等比例缩放参数  
$resizeParam = array (  
        'imagepath' => 'd:/xampp/www/', //图片存储路径  
'imageW' => 200, //图片存储路径  
'imageH' => 200 //图片存储路径  
);  
if (!empty ($array)) {
   Image :: getInstance()->uploadresize($array, $resizeParam);  
}
?>  
4.简单form表单

<html>  
<body>  
<form action='ImageTools.php' method='post' name='form' enctype="multipart/form-data">  
<input type='file' name='photo'>  
<input type=submit value='submit'>  
</form>  
</body>  
</html>  

over

相关参考:

http://blog.51yip.com/php/645.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-355788-1-1.html 上篇帖子: PHP模块:Memcached > Memcache 下篇帖子: 迅雷面试php开发
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表