缩放图片

      //使用前还需要在上传图片时对图片大小,尺寸做出限制
 // ini_set("memory_limit","20 M"); 图片达到2 M 的话    
   /**
     * 新建裁剪缩略图
     * @param
     *
     * @return int flag 0-缩放失败,1-成功,2-不需缩放,3-不需要缩放
     * ini_set("memory_limit","20 M"); 图片过大的话
     */
    public static function processImageCreate($oldWidth, $oldHeight, $newWidth, $newHeight, $srcImage, $newImgName, $cut=false) {
        $thumb_y = 0;
        $thumb_x = 0;
        //是否需要缩放
        if ($oldWidth > $newWidth || $oldHeight > $newHeight) {
            // 裁剪
            if ($cut) {
                //按小的边缩放
                if ($oldWidth < $oldHeight) {
                    $thumbWidth = $newWidth;
                    $thumbHeight = $oldHeight * $newWidth / $oldWidth;
                    // 裁剪
                    if ($thumbHeight > $newHeight) {
                        //$thumb_y        = -($thumbHeight - $newHeight)/2;

                    } else {
                        $thumbHeight = $newHeight;
                        $thumbWidth = $oldWidth * $newHeight / $oldHeight;
                        //$newHeight = $thumbHeight;
                    }
                } else {
                    $thumbHeight = $newHeight;
                    $thumbWidth = $oldWidth * $newHeight / $oldHeight;
                    if ($thumbWidth > $newWidth) {
                        //$thumb_x = -($thumbWidth - $newWidth) / 2;
                    } else {
                        $thumbWidth = $newWidth;
                        $thumbHeight = $oldHeight * $newWidth / $oldWidth;
                        //$newWidth = $thumbWidth;
                    }
                }

                // 仅缩放
            } else {
                if ($oldWidth > $oldHeight) {
                    $thumbWidth = $newWidth;
                    $thumbHeight = $oldHeight * $newWidth / $oldWidth;
                    $newHeight = $thumbHeight;
                } else {
                    $thumbHeight = $newHeight;
                    $thumbWidth = $oldWidth * $newHeight / $oldHeight;
                    $newWidth = $thumbWidth;
                }
            }

            // 新建空图片
            $thumb = imagecreatetruecolor($newWidth, $newHeight);
            // 组合新图片
            @imagecopyresampled($thumb, $srcImage, $thumb_x, $thumb_y, 0, 0, $thumbWidth, $thumbHeight, $oldWidth, $oldHeight);
            if (!imagejpeg($thumb, $newImgName, 100)) {
                $flag = 0;
            } else {
                $flag = 1;
            }
        } else if ($oldWidth == $newWidth || $oldHeight == $newHeight) {
            $flag = 2;
        } else {
            $flag = 3;
        }
        return $flag;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值