图像大小调整PHP
我正在尝试创建一个简单的图像大小调整功能,您能告诉我哪里出了问题:图像大小调整PHP,php,image-resizing,Php,Image Resizing,我正在尝试创建一个简单的图像大小调整功能,您能告诉我哪里出了问题: //resize image $imageSrc = imagecreatefromjpeg('http://www.katsmurals.com/cms/'.$target_path); $width = imagesx($imageSrc); $height = imagesy($imageSrc); echo $width; echo $height; //new dimensions $
//resize image
$imageSrc = imagecreatefromjpeg('http://www.katsmurals.com/cms/'.$target_path);
$width = imagesx($imageSrc);
$height = imagesy($imageSrc);
echo $width;
echo $height;
//new dimensions
$i = $width;
$j = $height;
while($i > 700){
$i = $i / 1.5;
$j = $j / 1.5;
}
$image_p = imagecreatetruecolor($i, $j);
$image = imagecreatefromjpeg('http://www.katsmurals.com/cms/'.$target_path);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $i, $j, $width, $height);
$newImage = $target_path.'_scaled.jpg';
imagejpeg($image_p, $newImage, 80);
$target_path是新上传的图像,如果宽度超过700,我想调整它的大小
完成此操作后,数据库将使用信息进行更新,此时图像上传正常,但大小似乎完全相同,因此调整大小的代码不起作用?
imagecopyresampled
的第一个参数是resource(image),而不是包含文件名的字符串。这是一个非常有用且令人惊奇的代码:
<?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 ) {
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) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
}
?>
链接:
我觉得很有帮助。希望对你有好处。
快乐编码 我已经更改了代码,仍然没有更改,你能看到我哪里出了问题吗?@user请进行一些调试。什么是
宽度
和高度
值?您正在调整哪种图像的大小?什么格式?.jpg文件,当回显宽度和高度时,这些是结果:宽度:1235高度:1551 while循环成功运行以提供新值,创建新的调整大小的图像是个问题。我设法让它工作起来,我已经为ayone发布了代码,ayone可能在将来需要它,可能会重复