我用过一些php实现的图片大小调整(image resize)函数,但是都不是很完美,有的图片在调整之后完全变形,还有的无缘无故多了一些线条,有的在颜色亮度上也差强人意。今天我重新找了下,又有了一些收获。if(isset($_FILES['manager_photo']) && !empty($_FILES['manager_photo']['name']))

{

$photo_name=$manager_name."-".$_FILES["manager_photo"]["name"];

$tmpname = $_FILES['manager_photo']['tmp_name'];

$file_ext= substr($photo_name, strripos($photo_name, '.'));

$dir = "./manager_photos/";

// finally resize

@img_resize( $tmpname , 90 , $dir , $photo_name, 1);

}

function img_resize( $tmpname, $size, $save_dir, $save_name, $maxisheight = 0 )

{

$save_dir .= ( substr($save_dir,-1) != "/") ? "/" : "";

$gis = getimagesize($tmpname);

$type = $gis[2];

switch($type)

{

case "1": $imorig = imagecreatefromgif($tmpname); break;

case "2": $imorig = imagecreatefromjpeg($tmpname);break;

case "3": $imorig = imagecreatefrompng($tmpname); break;

default: $imorig = imagecreatefromjpeg($tmpname);

}

$x = imagesx($imorig);

$y = imagesy($imorig);

$woh = (!$maxisheight)? $gis[0] : $gis[1] ;

if($woh <= $size)

{

$aw = $x;

$ah = $y;

}

else

{

if(!$maxisheight){

$aw = $size;

$ah = $size * $y / $x;

} else {

$aw = $size * $x / $y;

$ah = $size;

}

}

$im = imagecreatetruecolor($aw,$ah);

if (imagecopyresampled($im,$imorig , 0,0,0,0,$aw,$ah,$x,$y))

if (imagejpeg($im, $save_dir.$save_name))

return true;

else

return false;

}//img_resize

用ImageMagick来实现

有人提到用ImageMagick来实现,它里面有很多选项,我们可以用很少的代码就能实现图片调整。值得研究下。$image = new Imagick($src);

$image->resizeImage($width,$height,Imagick::FILTER_LANCZOS,1);

$image->cropImage($width_2, $height_2, $x, $y);

$image->setCompression(Imagick::COMPRESSION_JPEG);

$image->setImageCompressionQuality(82);

$image->writeImage($dest);

$image->destroy();

用php_class_upload实现

如果如上介绍不适合你,你不单能图片上传和图片调整,还可以切、割、加水印、旋转、翻转等等。

其他实现代码参考

此代码可以返回图片以及正确的名称,但是只能是黑色的?function resize($originalImage){

list($width, $height) = getimagesize($originalImage);

$newName=basename($originalImage);

$imageResized = imagecreatetruecolor(128, 128);

$imageTmp = imagecreatefromjpeg($originalImage);

imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, 128, 128, $width, $height);

imagejpeg($imageResized, "resizedImg/$newName",100);

imageDestroy($imageResized);

}

代码改善后,看看如下代码,可以设置颜色。function resizeImage($file){

define ('MAX_WIDTH', 1500);//max image width

define ('MAX_HEIGHT', 1500);//max image height

define ('MAX_FILE_SIZE', 10485760);

//iamge save path

$path = 'storeResize/';

//size of the resize image

$new_width = 128;

$new_height = 128;

//name of the new image

$nameOfFile = 'resize_'.$new_width.'x'.$new_height.'_'.basename($file['name']);

$image_type = $file['type'];

$image_size = $file['size'];

$image_error = $file['error'];

$image_file = $file['tmp_name'];

$image_name = $file['name'];

$image_info = getimagesize($image_file);

//check image type

if ($image_info['mime'] == 'image/jpeg' or $image_info['mime'] == 'image/jpg'){

}

else if ($image_info['mime'] == 'image/png'){

}

else if ($image_info['mime'] == 'image/gif'){

}

else{

//set error invalid file type

}

if ($image_error){

//set error image upload error

}

if ( $image_size > MAX_FILE_SIZE ){

//set error image size invalid

}

switch ($image_info['mime']) {

case 'image/jpg': //This isn't a valid mime type so we should probably remove it

case 'image/jpeg':

$image = imagecreatefromjpeg ($image_file);

break;

case 'image/png':

$image = imagecreatefrompng ($image_file);

break;

case 'image/gif':

$image = imagecreatefromgif ($image_file);

break;

}

if ($new_width == 0 && $new_height == 0) {

$new_width = 100;

$new_height = 100;

}

// ensure size limits can not be abused

$new_width = min ($new_width, MAX_WIDTH);

$new_height = min ($new_height, MAX_HEIGHT);

//get original image h/w

$width = imagesx ($image);

$height = imagesy ($image);

//$align = 'b';

$zoom_crop = 1;

$origin_x = 0;

$origin_y = 0;

//TODO setting Memory

// generate new w/h if not provided

if ($new_width && !$new_height) {

$new_height = floor ($height * ($new_width / $width));

} else if ($new_height && !$new_width) {

$new_width = floor ($width * ($new_height / $height));

}

// scale down and add borders

if ($zoom_crop == 3) {

$final_height = $height * ($new_width / $width);

if ($final_height > $new_height) {

$new_width = $width * ($new_height / $height);

} else {

$new_height = $final_height;

}

}

// create a new true color image

$canvas = imagecreatetruecolor ($new_width, $new_height);

imagealphablending ($canvas, false);

if (strlen ($canvas_color) < 6) {

$canvas_color = 'ffffff';

}

$canvas_color_R = hexdec (substr ($canvas_color, 0, 2));

$canvas_color_G = hexdec (substr ($canvas_color, 2, 2));

$canvas_color_B = hexdec (substr ($canvas_color, 2, 2));

// Create a new transparent color for image

$color = imagecolorallocatealpha ($canvas, $canvas_color_R, $canvas_color_G,$canvas_color_B, 127);

// Completely fill the background of the new image with allocated color.

imagefill ($canvas, 0, 0, $color);

// scale down and add borders

if ($zoom_crop == 2) {

$final_height = $height * ($new_width / $width);

if ($final_height > $new_height) {

$origin_x = $new_width / 2;

$new_width = $width * ($new_height / $height);

$origin_x = round ($origin_x - ($new_width / 2));

} else {

$origin_y = $new_height / 2;

$new_height = $final_height;

$origin_y = round ($origin_y - ($new_height / 2));

}

}

// Restore transparency blending

imagesavealpha ($canvas, true);

if ($zoom_crop > 0) {

$src_x = $src_y = 0;

$src_w = $width;

$src_h = $height;

$cmp_x = $width / $new_width;

$cmp_y = $height / $new_height;

// calculate x or y coordinate and width or height of source

if ($cmp_x > $cmp_y) {

$src_w = round ($width / $cmp_x * $cmp_y);

$src_x = round (($width - ($width / $cmp_x * $cmp_y)) / 2);

} else if ($cmp_y > $cmp_x) {

$src_h = round ($height / $cmp_y * $cmp_x);

$src_y = round (($height - ($height / $cmp_y * $cmp_x)) / 2);

}

// positional cropping!

if ($align) {

if (strpos ($align, 't') !== false) {

$src_y = 0;

}

if (strpos ($align, 'b') !== false) {

$src_y = $height - $src_h;

}

if (strpos ($align, 'l') !== false) {

$src_x = 0;

}

if (strpos ($align, 'r') !== false) {

$src_x = $width - $src_w;

}

}

// positional cropping!

imagecopyresampled ($canvas, $image, $origin_x, $origin_y, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h);

} else {

imagecopyresampled ($canvas, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

}

//Straight from Wordpress core code. Reduces filesize by up to 70% for PNG's

if ( (IMAGETYPE_PNG == $image_info[2] || IMAGETYPE_GIF == $image_info[2]) && function_exists('imageistruecolor') && !imageistruecolor( $image ) && imagecolortransparent( $image ) > 0 ){

imagetruecolortopalette( $canvas, false, imagecolorstotal( $image ) );

}

$quality = 100;

$nameOfFile = 'resize_'.$new_width.'x'.$new_height.'_'.basename($file['name']);

if (preg_match('/^image\/(?:jpg|jpeg)$/i', $image_info['mime'])){

imagejpeg($canvas, $path.$nameOfFile, $quality);

} else if (preg_match('/^image\/png$/i', $image_info['mime'])){

imagepng($canvas, $path.$nameOfFile, floor($quality * 0.09));

} else if (preg_match('/^image\/gif$/i', $image_info['mime'])){

imagegif($canvas, $path.$nameOfFile);

}

}

php resize函数,php调整图片大小的image resize函数详解相关推荐

  1. Linux中如何使用gThumb批量调整图片大小

    2019独角兽企业重金招聘Python工程师标准>>> 由于主要步骤都以 gThumb 为中心,所以请先确保你在系统中已经安装了该应用.如果没有,可以自行在 Ubuntu Softw ...

  2. 服务器里怎么更改网站图片大小,php实现在服务器端调整图片大小的方法

    本文实例讲述了php实现在服务器端调整图片大小的方法.分享给大家供大家参考.具体分析如下: 在服务器端完成图片大小的调整,会比在浏览器的处理有很多的好处. 本文介绍了PHP如何在服务器端调整图片大小. ...

  3. java 图片 大小_在JAVA中调整图片大小

    在JAVA中,当我们调整图片的大小或比例时,我们可以按照以下的步棸: 通过调用的ImageIO类的read(File)方法 创建用于输入图片BufferedImage对象. 按所需输出高度输出的Buf ...

  4. 怎么调整图片大小会不变形?

    怎么调整图片大小会不变形? 要保证图片不变形做边填充是一个不错的选择.图片边填充 是指在保证图片原始宽高比例不变的情况下,按照指定图片高和宽进行图片展示,对于宽高不够的进行等比例填充,也就经常说的pa ...

  5. Python「PIL」:调整图片大小

    使用 PIL 在图片比例不变的情况下修改图片大小. 目录 一.介绍 二.代码 一.介绍 Image.resize def resize(self, size, resample=BICUBIC, bo ...

  6. 利用CSS调整图片大小

    通常,我们可以给图片<img>设置一个CSS属性,定义其高度和宽度.但有时候,我们只希望控制图片的最大可见大小.这样的操作,一般有两种办法:1.直接使用CSS属性值:2.使用JavaScr ...

  7. plt.matshow怎样调整图片大小

    plt.matshow函数可以用来显示矩阵图像,要调整图片大小,可以使用figsize参数来设置图片大小.例如: plt.matshow(matrix, figsize=(width, height) ...

  8. python调整图片大小不覆盖exif_python---基础知识回顾(十一)图像处理模块PIL

    前戏: 虽然PIL没有入OpenCV那样强大的功能,但是所提供的功能,在一般的图像处理中足够使用. 图像类别: 计算机绘图中有两类图像:一类是矢量图,另一类是点阵图(位图) 矢量图: 基于计算机数字对 ...

  9. Python调整图片大小并保存调整后的图像

    Python调整图片大小并保存调整后的图像 目录 Python调整图片大小并保存调整后的图像 #原始图像

最新文章

  1. 世界一流大学真的建成了?26所“985”高校自评报告出炉
  2. 【转】POJ 2104 K-th Number(2)
  3. 网球hcc http catcher使用方法以及规则分享
  4. 深入理解java虚拟机---读后笔记(垃圾回收)
  5. Oracle客户端安装教程
  6. nginx启动成功,解决别的电脑访问不了页面的问题
  7. 美国服务器百度抓取耗时不稳定,百度或者其他搜索引擎抓取频次快慢的因素,还会受什么有影响?...
  8. SystemTap Errors Introduce
  9. qt传值给js及js传值给qt(qt及js的交互)
  10. 三星平板电脑html文件放在哪里,三星Tab3怎么连接电脑?三星Tab3平板电脑连接电脑的方法图解...
  11. r语言如何计算t分布临界值_医学统计与R语言:四格表卡方还需要连续校正吗?...
  12. Debian10.6 Xfce 系统安装教程
  13. android 手机 用短信发pdf文件,iPhone如何将PDF通过短信邮件发给别人【仅限iPhone6/6s】...
  14. PAT 乙级 1065 单身狗 (25 分)
  15. 吐血整理!140种Python标准库、第三方库和外部工具都有了
  16. 深度学习 | 训练及优化方法
  17. PE文件资源解析(十一)对话框资源的解析
  18. LeetCode 176 第二高的薪水
  19. 如何评价《就算老公一毛钱股份都没拿到,在我心里,他依然是最牛逼的创业者》里面这位CEO的所作所为?
  20. SwiftUI脑洞大开打造实时显示当前值的Slider(滑动器)

热门文章

  1. 十大热销HR软件实施优势和风险比较
  2. #每天一道ACM_8.8(给草坪装洒水装置)
  3. 全球及中国工业结晶器行业运营动态及投资潜力预测报告2022~2027年
  4. 培养孩子形成六大好习惯
  5. C++之状态(State)模式
  6. “海底长城”合龙!揭秘深中海底隧道中的无损检测技术
  7. Linux网络系统的管理与应用
  8. 前端初学者----网页中的表格
  9. 单片机毕设 STM32 wifi照明控制系统 - 智能路灯(毕设分享)
  10. centos7升级pip版本