php裁剪图片

gd库要开启

<?php

/**
* Author : smallchicken
* Time : 2009年6月8日16:46:05
* mode 1 : 强制裁剪,生成图片严格按照需要,不足放大,超过裁剪,图片始终铺满
* mode 2 : 和1类似,但不足的时候 不放大 会产生补白,可以用png消除。
* mode 3 : 只缩放,不裁剪,保留全部图片信息,会产生补白,
* mode 4 : 只缩放,不裁剪,保留全部图片信息,生成图片大小为最终缩放后的图片有效信息的实际大小,不产生补白
* 默认补白为白色,如果要使补白成透明像素,请使用SaveAlpha()方法代替SaveImage()方法
*
* 调用方法:
*/
$ic=new ImageCrop(‘a.jpg‘,‘afterCrop.jpg‘);
$ic->Crop(480,600,1);
$ic->SaveImage();
//$ic->SaveAlpha();将补白变成透明像素保存
$ic->destory();

class ImageCrop{

var $sImage;
var $dImage;
var $src_file;
var $dst_file;
var $src_width;
var $src_height;
var $src_ext;
var $src_type;

function ImageCrop($src_file,$dst_file=‘‘){
$this->src_file=$src_file;
$this->dst_file=$dst_file;
$this->LoadImage();
}

function SetSrcFile($src_file){
$this->src_file=$src_file;
}

function SetDstFile($dst_file){
$this->dst_file=$dst_file;
}

function LoadImage(){
list($this->src_width, $this->src_height, $this->src_type) = getimagesize($this->src_file);
switch($this->src_type) {
case IMAGETYPE_JPEG :
$this->sImage=imagecreatefromjpeg($this->src_file);
$this->ext=‘jpg‘;
break;
case IMAGETYPE_PNG :
$this->sImage=imagecreatefrompng($this->src_file);
$this->ext=‘png‘;
break;
case IMAGETYPE_GIF :
$this->sImage=imagecreatefromgif($this->src_file);
$this->ext=‘gif‘;
break;
default:
exit();
}
}

function SaveImage($fileName=‘‘){
$this->dst_file=$fileName ? $fileName : $this->dst_file;
switch($this->src_type) {
case IMAGETYPE_JPEG :
imagejpeg($this->dImage,$this->dst_file,100);
break;
case IMAGETYPE_PNG :
imagepng($this->dImage,$this->dst_file);
break;
case IMAGETYPE_GIF :
imagegif($this->dImage,$this->dst_file);
break;
default:
break;
}
}

function OutImage(){
switch($this->src_type) {
case IMAGETYPE_JPEG :
header(‘Content-type: image/jpeg‘);
imagejpeg($this->dImage);
break;
case IMAGETYPE_PNG :
header(‘Content-type: image/png‘);
imagepng($this->dImage);
break;
case IMAGETYPE_GIF :
header(‘Content-type: image/gif‘);
imagegif($this->dImage);
break;
default:
break;
}
}

function SaveAlpha($fileName=‘‘){
$this->dst_file=$fileName ? $fileName . ‘.png‘ : $this->dst_file .‘.png‘;
imagesavealpha($this->dImage, true);
imagepng($this->dImage,$this->dst_file);
}

function OutAlpha(){
imagesavealpha($this->dImage, true);
header(‘Content-type: image/png‘);
imagepng($this->dImage);
}

function destory(){
imagedestroy($this->sImage);
imagedestroy($this->dImage);
}

function Crop($dst_width,$dst_height,$mode=1,$dst_file=‘‘){
if($dst_file) $this->dst_file=$dst_file;
$this->dImage = imagecreatetruecolor($dst_width,$dst_height);

$bg = imagecolorallocatealpha($this->dImage,255,255,255,127);
imagefill($this->dImage, 0, 0, $bg);
imagecolortransparent($this->dImage,$bg);

$ratio_w=1.0 * $dst_width / $this->src_width;
$ratio_h=1.0 * $dst_height / $this->src_height;
$ratio=1.0;
switch($mode){
case 1: // always crop
if( ($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1)){
$ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w;
$tmp_w = (int)($dst_width / $ratio);
$tmp_h = (int)($dst_height / $ratio);
$tmp_img=imagecreatetruecolor($tmp_w , $tmp_h);
$src_x = (int) (($this->src_width-$tmp_w)/2) ;
$src_y = (int) (($this->src_height-$tmp_h)/2) ;
imagecopy($tmp_img, $this->sImage, 0,0,$src_x,$src_y,$tmp_w,$tmp_h);
imagecopyresampled($this->dImage,$tmp_img,0,0,0,0,$dst_width,$dst_height,$tmp_w,$tmp_h);
imagedestroy($tmp_img);
}else{
$ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w;
$tmp_w = (int)($this->src_width * $ratio);
$tmp_h = (int)($this->src_height * $ratio);
$tmp_img=imagecreatetruecolor($tmp_w ,$tmp_h);
imagecopyresampled($tmp_img,$this->sImage,0,0,0,0,$tmp_w,$tmp_h,$this->src_width,$this->src_height);
$src_x = (int)($tmp_w - $dst_width) / 2 ;
$src_y = (int)($tmp_h - $dst_height) / 2 ;
imagecopy($this->dImage, $tmp_img, 0,0,$src_x,$src_y,$dst_width,$dst_height);
imagedestroy($tmp_img);
}
break;
case 2: // only small
if($ratio_w < 1 && $ratio_h < 1){
$ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w;
$tmp_w = (int)($dst_width / $ratio);
$tmp_h = (int)($dst_height / $ratio);
$tmp_img=imagecreatetruecolor($tmp_w , $tmp_h);
$src_x = (int) ($this->src_width-$tmp_w)/2 ;
$src_y = (int) ($this->src_height-$tmp_h)/2 ;
imagecopy($tmp_img, $this->sImage, 0,0,$src_x,$src_y,$tmp_w,$tmp_h);
imagecopyresampled($this->dImage,$tmp_img,0,0,0,0,$dst_width,$dst_height,$tmp_w,$tmp_h);
imagedestroy($tmp_img);
}elseif($ratio_w > 1 && $ratio_h > 1){
$dst_x = (int) abs($dst_width - $this->src_width) / 2 ;
$dst_y = (int) abs($dst_height -$this->src_height) / 2;
imagecopy($this->dImage, $this->sImage,$dst_x,$dst_y,0,0,$this->src_width,$this->src_height);
}else{
$src_x=0;$dst_x=0;$src_y=0;$dst_y=0;
if(($dst_width - $this->src_width) < 0){
$src_x = (int) ($this->src_width - $dst_width)/2;
$dst_x =0;
}else{
$src_x =0;
$dst_x = (int) ($dst_width - $this->src_width)/2;
}

if( ($dst_height -$this->src_height) < 0){
$src_y = (int) ($this->src_height - $dst_height)/2;
$dst_y = 0;
}else{
$src_y = 0;
$dst_y = (int) ($dst_height - $this->src_height)/2;
}
imagecopy($this->dImage, $this->sImage,$dst_x,$dst_y,$src_x,$src_y,$this->src_width,$this->src_height);
}
break;
case 3: // keep all image size and create need size
if($ratio_w > 1 && $ratio_h > 1){
$dst_x = (int)(abs($dst_width - $this->src_width )/2) ;
$dst_y = (int)(abs($dst_height- $this->src_height)/2) ;
imagecopy($this->dImage, $this->sImage, $dst_x,$dst_y,0,0,$this->src_width,$this->src_height);
}else{
$ratio = $ratio_w > $ratio_h ? $ratio_h : $ratio_w;
$tmp_w = (int)($this->src_width * $ratio);
$tmp_h = (int)($this->src_height * $ratio);
$tmp_img=imagecreatetruecolor($tmp_w ,$tmp_h);
imagecopyresampled($tmp_img,$this->sImage,0,0,0,0,$tmp_w,$tmp_h,$this->src_width,$this->src_height);
$dst_x = (int)(abs($tmp_w -$dst_width )/2) ;
$dst_y = (int)(abs($tmp_h -$dst_height)/2) ;
imagecopy($this->dImage, $tmp_img, $dst_x,$dst_y,0,0,$tmp_w,$tmp_h);
imagedestroy($tmp_img);
}
break;
case 4: // keep all image but create actually size
if($ratio_w > 1 && $ratio_h > 1){
$this->dImage = imagecreatetruecolor($this->src_width,$this->src_height);
imagecopy($this->dImage, $this->sImage,0,0,0,0,$this->src_width,$this->src_height);
}else{
$ratio = $ratio_w > $ratio_h ? $ratio_h : $ratio_w;
$tmp_w = (int)($this->src_width * $ratio);
$tmp_h = (int)($this->src_height * $ratio);
$this->dImage = imagecreatetruecolor($tmp_w ,$tmp_h);
imagecopyresampled($this->dImage,$this->sImage,0,0,0,0,$tmp_w,$tmp_h,$this->src_width,$this->src_height);
}
break;
}
}// end Crop

}
?>

时间: 2024-08-15 10:52:43

php裁剪图片的相关文章

自定义裁剪图片

判断图片大小,根据裁剪目标对图片进行缩放,然后裁剪图片(在图像不变形情况下) /// <summary> /// 指定长宽裁剪 /// 按模版比例最大范围的裁剪图片并缩放至模版尺寸 /// </summary> /// <param name="fromFile">原图Stream对象</param> /// <param name="fileSaveUrl">保存路径</param> ///

Quartz2D练习 -- 裁剪图片分类

Main.storyboard <?xml version="1.0" encoding="UTF-8" standalone="no"?> <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5053" systemVers

IOS开发——UI进阶篇(十八)核心动画小例子,转盘(裁剪图片、自定义按钮、旋转)图片折叠、音量震动条、倒影、粒子效果

一.转盘(裁剪图片.自定义按钮.旋转) 1.裁剪图片 将一张大图片裁剪为多张 // CGImageCreateWithImageInRect:用来裁剪图片 // image:需要裁剪的图片 // rect:裁剪图片的尺寸,传递是像素 CGImageRef norImage = CGImageCreateWithImageInRect(norBigImage.CGImage, clipRect); 2.每次点击按钮立马变为选中状态,并且取消上次的按钮的选中状态 当然还要重写- (void)setH

IOS 按比例裁剪图片

拍照或者从图片库中获取图片 操作过程中容易闪退,也总会有内存压力警告,第一步,首先可以考虑裁剪图片,实际上可能不需要那么大的.其次可以考虑把耗时的比如存储过程放进线程. 这里封装裁剪图片的类方法. //NavView.m #define IMAGE_MAX_SIZE_WIDTH 640 #define IMAGE_MAX_SIZE_GEIGHT 1136 +(UIImage *)fitSmallImage:(UIImage *)image { if (nil == image) { return

MVC使用JCrop上传、裁剪图片

JCrop用来裁剪图片,本篇想体验的是: 在视图页上传图片: 上传成功,跳转到另外一个编辑视图页,使用JCrop对该图片裁剪,并保存图片到指定文件夹: 裁剪成功后,在主视图页显示裁剪图片: 当然,实际项目中最有可能的做法是:在本页上传.裁剪并保存. □ 思路 →在上传图片视图页,把图片上传保存到一个临时文件夹Upload→在编辑裁剪视图页,点击"裁剪"按钮,把JCrop能提供的参数,比如宽度.高度.离顶部距离,离底部距离,离左右端距离等封装成类,传递给控制器方法→控制器方法根据接收到的

Android拍照、相册选取、裁剪图片

来自:http://blog.csdn.net/ryantang03/article/details/8656278 package com.example.listactivity; import java.io.ByteArrayOutputStream; import java.io.File; import com.example.model.ImageTools; import android.app.Activity; import android.app.AlertDialog;

调用 android 系统拍照结合 android-crop 裁剪图片

在一个应用中更换用户的头像,一般有拍照和从图库中选择照片两种方法,现在网上也有很多开源的,但是很多都太复杂.而 Android-crop 这个库比较小,代码不复杂,比较适合,但是它没有拍照这个功能,需要我们自己整合进去. 调用系统相机拍照 返回略缩图的拍照 // 调用系统的拍照 private void dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTU

Android 从图库选择图片,拍照图片,裁剪图片

我直接写代码了 1先来几个常亮 private static final int PHOTO_REQUEST_CAREMA = 1;// 拍照private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择private static final int PHOTO_REQUEST_CUT = 3;// 结果private static final String PHOTO_FILE_NAME = "temp_photo.jpg"

UIImage 裁剪图片和等比列缩放图片

本文转载至 http://blog.csdn.net/cuiweijie3/article/details/9514293 转自 http://www.tedz.me/ios/uiimage-crop-resize-image @interface UIImage(UIImageScale) -(UIImage*)getSubImage:(CGRect)rect; -(UIImage*)scaleToSize:(CGSize)size; @end @implementation UIImage(

HTML5 本地裁剪图片并上传至服务器(转)

很多情况下用户上传的图片都需要经过裁剪,比如头像啊什么的.但以前实现这类需求都很复杂,往往需要先把图片上传到服务器,然后返回给用户,让用户确定裁剪坐标,发送给服务器,服务器裁剪完再返回给用户,来回需要 5 步.步骤繁琐不说,当很多用户上传图片的时候也很影响服务器性能. HTML5 的出现让我们可以更方便的实现这一需求.虽然这里所说的技术都貌似有点过时了(前端界的“过时”,你懂的),但还是有些许参考价值.在这里我只说一下要点,具体实现同学们慢慢研究. 下面奉上我自己写的一个demo,在输入框中选好