我想创建一个非常基本的上传,调整大小和裁剪PHP脚本。 这个功能与Twitter用于上传头像图片的方法完全相同(最后我检查过)。
我希望脚本可以拍摄任意大小的图像,将最短边调整为116px,然后裁剪掉顶部和底部(或左右边,如果是横向),以获得116px×116px的正方形。
我不想要一个膨胀的PHP脚本,客户端调整大小或任何东西,只需一个简单的PHP调整大小和裁剪。这是怎么做到的?
答案 0 :(得分:4)
GD图书馆是一个很好的起点。
答案 1 :(得分:4)
有一个名为PHP Image Magician的简单易用的开源库。它使用GD但将其使用简化为3行。
基础用法示例:
$magicianObj = new imageLib('racecar.jpg');
$magicianObj -> resizeImage(100, 200, 'crop');
$magicianObj -> saveImage('racecar_small.png');
答案 2 :(得分:2)
如果你想从我的上传工作一个例子,调整大小和裁剪类会做所有这些以及其他一些很酷的东西 - 你可以在需要时使用它们或只是把你喜欢的东西拿出来:
http://www.mjdigital.co.uk/blog/php-upload-and-resize-image-class/
我觉得它太臃肿了! - 你可以做这件事(未经测试):
if((isset($_FILES['file']['error']))&&($_FILES['file']['error']==0)){ // if a file has been posted then upload it include('INCLUDE_CLASS_FILE_HERE.php'); $myImage = new _image; // upload image $myImage->uploadTo = 'uploads/'; // SET UPLOAD FOLDER HERE $myImage->returnType = 'array'; // RETURN ARRAY OF IMAGE DETAILS $img = $myImage->upload($_FILES['file']); if($img) { $myImage->newWidth = 116; $myImage->newHeight = 116; $i = $myImage->resize(); // resizes to 116px keeping aspect ratio // get new image height $imgWidth = $i['width']; // get new image width $imgHeight = $i['height']; if($i) { // work out where to crop it $cropX = ($imgWidth>116) ? (($imgWidth-116)/2) : 0; $cropY = ($imgHeight>116) ? (($imgHeight-116)/2) : 0; $cropped = $myImage->crop(116,116,$cropX,$cropY); if($cropped) { echo 'It Worked (I think!)'; print_r($cropped); } else { echo 'Crop failed'; } } else { echo 'Resize failed'; } } else { echo 'Upload failed'; }
答案 3 :(得分:0)
我制作了这个非常简单易用的功能,它允许您调整图像的大小,裁剪和居中到特定的宽度和高度,它可以支持JPG,PNG和GIF。您可以将其复制并粘贴到您的代码中:
function resize_imagejpg($file, $w, $h, $finaldst) {
list($width, $height) = getimagesize($file);
$src = imagecreatefromjpeg($file);
$ir = $width/$height;
$fir = $w/$h;
if($ir >= $fir){
$newheight = $h;
$newwidth = $w * ($width / $height);
}
else {
$newheight = $w / ($width/$height);
$newwidth = $w;
}
$xcor = 0 - ($newwidth - $w) / 2;
$ycor = 0 - ($newheight - $h) / 2;
$dst = imagecreatetruecolor($w, $h);
imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, $newheight,
$width, $height);
imagejpeg($dst, $finaldst);
imagedestroy($dst);
return $file;
}
function resize_imagegif($file, $w, $h, $finaldst) {
list($width, $height) = getimagesize($file);
$src = imagecreatefromgif($file);
$ir = $width/$height;
$fir = $w/$h;
if($ir >= $fir){
$newheight = $h;
$newwidth = $w * ($width / $height);
}
else {
$newheight = $w / ($width/$height);
$newwidth = $w;
}
$xcor = 0 - ($newwidth - $w) / 2;
$ycor = 0 - ($newheight - $h) / 2;
$dst = imagecreatetruecolor($w, $h);
$background = imagecolorallocatealpha($dst, 0, 0, 0, 127);
imagecolortransparent($dst, $background);
imagealphablending($dst, false);
imagesavealpha($dst, true);
imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, $newheight,
$width, $height);
imagegif($dst, $finaldst);
imagedestroy($dst);
return $file;
}
function resize_imagepng($file, $w, $h, $finaldst) {
list($width, $height) = getimagesize($file);
$src = imagecreatefrompng($file);
$ir = $width/$height;
$fir = $w/$h;
if($ir >= $fir){
$newheight = $h;
$newwidth = $w * ($width / $height);
}
else {
$newheight = $w / ($width/$height);
$newwidth = $w;
}
$xcor = 0 - ($newwidth - $w) / 2;
$ycor = 0 - ($newheight - $h) / 2;
$dst = imagecreatetruecolor($w, $h);
$background = imagecolorallocate($dst, 0, 0, 0);
imagecolortransparent($dst, $background);
imagealphablending($dst, false);
imagesavealpha($dst, true);
imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth,
$newheight,$width, $height);
imagepng($dst, $finaldst);
imagedestroy($dst);
return $file;
}
function ImageResize($file, $w, $h, $finaldst) {
$getsize = getimagesize($file);
$image_type = $getsize[2];
if( $image_type == IMAGETYPE_JPEG) {
resize_imagejpg($file, $w, $h, $finaldst);
} elseif( $image_type == IMAGETYPE_GIF ) {
resize_imagegif($file, $w, $h, $finaldst);
} elseif( $image_type == IMAGETYPE_PNG ) {
resize_imagepng($file, $w, $h, $finaldst);
}
}
使用它所需要做的就是这样称呼:
ImageResize(image, width, height, destination);
E.g。
ImageResize("uploads/face.png", 100, 150, "images/user332profilepic.png");