php实现图片居中裁剪并缩放
1 <?php 2 /* 3 ** author 李攀 4 ** email 1061589921@qq.com 5 ** 实现移动开发中,多张图片排列. 6 */ 7 $file = './234.png'; 8 $Image = new image($file); 9 $width = $Image->getImageWidth(); 10 $height = $Image->getImageHeight(); 11 //判断该相片是否长宽相等 12 if($width != $height){ 13 //不相等则先以最小边为长度截取图片中心部分 14 if($width > $height){ 15 $x = ($width - $height) / 2; 16 $y = 0; 17 $width = $height; 18 }else{ 19 $y = ($height - $width) / 2; 20 $x = 0; 21 $height = $width; 22 } 23 $Image->crop($file,$width,$height,$x,$y,$file); 24 } 25 26 //等比例缩放图片 27 $dst_w = 100; 28 $dst_h = 100; 29 $Image->reduce($file,$dst_w,$dst_h,$file); 30 ?> 31 <?php 32 class image{ 33 private $_width; 34 private $_height; 35 private $_type; 36 //实例化时获取图片信息 37 function __construct($file_url){ 38 $info = getimagesize($file_url); 39 $this->_width = $info[0]; 40 $this->_height = $info[1]; 41 $type = $info['mime']; 42 $type = explode('/',$type); 43 $this->_type = $type[1]; 44 } 45 //返回图片类型 46 public function getImageType(){ 47 return $this->_type; 48 } 49 //返回图片宽度 50 public function getImageWidth(){ 51 return $this->_width; 52 } 53 //返回图片高度 54 public function getImageHeight(){ 55 return $this->_height; 56 } 57 58 /* 59 **图片裁剪 60 ** $tmp_image源文件 61 ** $dst_w 裁剪后的图片的宽度 62 ** $dst_h 裁剪后的图片的高度 63 ** $x 在源图的$x处坐标开始裁剪 64 ** $y 在源图的$y处坐标开始裁剪 65 */ 66 public function crop($tmp_image,$dst_w,$dst_h,$x,$y,$path){ 67 switch($this->_type){ 68 case 'jpeg': 69 $src = imagecreatefromjpeg($tmp_image); 70 break; 71 case 'gif': 72 $src = imagecreatefromgif($tmp_image); 73 break; 74 case 'png': 75 $src = imagecreatefrompng($tmp_image); 76 break; 77 } 78 79 $dst = imagecreatetruecolor($dst_w,$dst_h); 80 $color = imagecolorallocate($dst,255,255,255); 81 imagecolortransparent($dst,$color); 82 imagefill($dst,0,0,$color); 83 $bool = imagecopyresampled($dst,$src, 0,0,$x,$y, $dst_w,$dst_h,$dst_w,$dst_h); 84 switch($this->_type){ 85 case 'jpeg': 86 imagejpeg($dst,$path,100); 87 break; 88 case 'gif': 89 imagegif($dst,$path); 90 break; 91 case 'png': 92 imagepng($dst,$path); 93 break; 94 } 95 96 imagedestroy($src); 97 imagedestroy($dst); 98 return $bool; 99 }100 101 /*102 ** 等比例缩放图片103 ** $tmp_image源图104 ** $dst_w 缩放后的图片宽度105 ** $dst_h 缩放后的图片高度106 */107 public function reduce($tmp_image,$dst_w,$dst_h,$path){108 switch($this->_type){109 case 'jpeg':110 $src = imagecreatefromjpeg($tmp_image);111 break;112 case 'gif':113 $src = imagecreatefromgif($tmp_image);114 break;115 case 'png':116 $src = imagecreatefrompng($tmp_image);117 break;118 }119 $imagex = imagesx($src);120 $imagey = imagesy($src);121 $dst = imagecreatetruecolor($dst_w,$dst_h);122 $color = imagecolorallocate($dst,255,255,255);123 imagecolortransparent($dst,$color);124 imagefill($dst,0,0,$color);125 $bool = imagecopyresampled($dst,$src,0,0,0,0,$dst_w,$dst_h,$imagex,$imagey);126 switch($this->_type){127 case 'jpeg':128 imagejpeg($dst,$path,100);129 break;130 case 'gif':131 imagegif($dst,$path);132 break;133 case 'png':134 imagepng($dst,$path);135 break;136 }137 imagedestroy($src);138 imagedestroy($dst);139 return $bool;140 }141 }

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

PHP日志记录对于监视和调试Web应用程序以及捕获关键事件,错误和运行时行为至关重要。它为系统性能提供了宝贵的见解,有助于识别问题并支持更快的故障排除

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸
