0
我一直在寻找遍及互联网寻找解决方案。我使用这个SimpleImage.php类来调整我在这里找到的图像的大小(http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php)。到目前为止,一切正常。如果我尝试重新调整图像的大小以适应其高度或宽度,它正在完成其工作。但是,这只适用于我试图一次调整图像大小的情况。假设我的图像的原始尺寸是1400x1100,并且我想将其高度调整为800.这将使我得到某些数字xx的结果。反之亦然。调整图像的最大宽度和高度
我的目标是根据我传递的最大宽度和最大高度调整图像大小。
事情是这样的:
$image = new SimpleImage();
$image->load($image_url);
$image->resizeToWidthHeight(MAX-WIDTH, MAX-HEIGHT);
$image->save('photo.png');
这里是我的SimpleImage.php类
<?php
class SimpleImage {
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if($this->image_type == IMAGETYPE_JPEG) {
$this->image = imagecreatefromjpeg($filename);
} elseif($this->image_type == IMAGETYPE_GIF) {
$this->image = imagecreatefromgif($filename);
} elseif($this->image_type == IMAGETYPE_PNG) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if($image_type == IMAGETYPE_JPEG) {
imagejpeg($this->image,$filename,$compression);
} elseif($image_type == IMAGETYPE_GIF) {
imagegif($this->image,$filename);
} elseif($image_type == IMAGETYPE_PNG) {
imagepng($this->image,$filename);
}
if($permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if($image_type == IMAGETYPE_JPEG) {
imagejpeg($this->image);
} elseif($image_type == IMAGETYPE_GIF) {
imagegif($this->image);
} elseif($image_type == IMAGETYPE_PNG) {
imageAlphaBlending($this->image, true);
imageSaveAlpha($this->image, true);
imagepng($this->image);
}
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height/$this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width/$this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
// ADDED CODE IS HERE - NOT SURE WHY IT DOESN'T WORK FOR PNG
// Setup new image
$new_image = imagecreatetruecolor($width, $height);
// These parameters are required for handling PNG files.
imagealphablending($new_image, false);
imagesavealpha($new_image,true);
$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
// Resize image
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
}
?>
你有任何其他的参考来帮助我吗? – Bono
我认为这是一个很好的:http://stackoverflow.com/questions/1855996/crop-image-in-php – Halcyon