dw中的php如何插图片大小,在上传PHP之前调整图像大小

这是我用来调整图像大小的代码.

在我的情况下,我给函数原始文件名,然后缩略图文件名.

您可以非常轻松地适应您的情况.

public static function GenerateThumbnail($im_filename,$th_filename,$max_width,$max_height,$quality = 0.75)

{

// The original image must exist

if(is_file($im_filename))

{

// Let's create the directory if needed

$th_path = dirname($th_filename);

if(!is_dir($th_path))

mkdir($th_path, 0777, true);

// If the thumb does not aleady exists

if(!is_file($th_filename))

{

// Get Image size info

list($width_orig, $height_orig, $image_type) = @getimagesize($im_filename);

if(!$width_orig)

return 2;

switch($image_type)

{

case 1: $src_im = @imagecreatefromgif($im_filename); break;

case 2: $src_im = @imagecreatefromjpeg($im_filename); break;

case 3: $src_im = @imagecreatefrompng($im_filename); break;

}

if(!$src_im)

return 3;

$aspect_ratio = (float) $height_orig / $width_orig;

$thumb_height = $max_height;

$thumb_width = round($thumb_height / $aspect_ratio);

if($thumb_width > $max_width)

{

$thumb_width = $max_width;

$thumb_height = round($thumb_width * $aspect_ratio);

}

$width = $thumb_width;

$height = $thumb_height;

$dst_img = @imagecreatetruecolor($width, $height);

if(!$dst_img)

return 4;

$success = @imagecopyresampled($dst_img,$src_im,0,0,0,0,$width,$height,$width_orig,$height_orig);

if(!$success)

return 4;

switch ($image_type)

{

case 1: $success = @imagegif($dst_img,$th_filename); break;

case 2: $success = @imagejpeg($dst_img,$th_filename,intval($quality*100)); break;

case 3: $success = @imagepng($dst_img,$th_filename,intval($quality*9)); break;

}

if(!$success)

return 4;

}

return 0;

}

return 1;

}

返回代码仅用于区分不同类型的错误.

通过回顾那段代码,我不喜欢“神奇的数字”技巧.我将不得不改变它(例如通过例外).

if (!empty($_FILES["pic$index"]["name"])) {

$ext = substr($_FILES["pic$index"]["name"], strrpos($_FILES["pic$index"]["name"], '.') + 1);

$dir = "../gallery/$mkdir";

// Move it

if(move_uploaded_file($_FILES["pic$index"]["tmp_name"] , "$dir/img-$index.$ext.tmp"))

{

// Resize it

GenerateThumbnail("$dir/img-$index.$ext.tmp","$dir/img-$index.$ext",600,800,0.80);

// Delete full size

unlink("$dir/img-$index.$ext.tmp");

}

}

使用move_uploaded_file移动它(推荐),然后你可以调整它并将其发送到它的最终目的地.您可能甚至不需要“.tmp”,您可以使用.

// Move it

if(move_uploaded_file($_FILES["pic$index"]["tmp_name"] , "$dir/img-$index.$ext"))

// Resize it

GenerateThumbnail("$dir/img-$index.$ext","$dir/img-$index.$ext",600,800);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值