0

我试图创建一个简单的图像调整大小功能,你能告诉我哪里出错了:

//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,我想调整它的大小。

完成此操作后,将使用信息更新数据库,此时图像上传正常,但大小似乎完全相同,因此调整大小代码不起作用?

4

2 回答 2

1

一个非常有用和惊人的代码:

   <?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;
   }      

}
?>

链接: http: //www.white-hat-web-design.co.uk/articles/php-image-resizing.php 我发现它很有帮助。希望对你有好处。编码快乐!!

于 2012-10-02T11:16:36.623 回答
0

http://php.net/manual/en/function.imagecopyresampled.php

的第一个参数imagecopyresampled是资源(图像),而不是包含文件名的字符串。

于 2010-07-18T11:31:37.533 回答