PHP 调整图像大小

发布于 2024-09-10 00:48:41 字数 764 浏览 2 评论 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,我想调整它的大小。

一旦完成,数据库就会更新根据信息,目前图像上传正常,但大小似乎完全相同,因此调整大小代码不起作用?

Im trying to create a simple image resize feature, can you tell me where i'm going wrong:

//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 is the newly uploaded image, i want to resize it if the width is over 700.

Once this is done, a DB is updated with the information, at the moment the image is being uploaded fine, but the size seems to be exactly the same, so the resizing code isn't working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零崎曲识 2024-09-17 00:48:41

非常有用且令人惊叹的代码:

   <?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
我发现它非常有帮助。希望对你有好处。
快乐编码!!

A very helpful and amazing code:

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

}
?>

Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
i found it very helpful.hope good for you.
Happy coding!!

野鹿林 2024-09-17 00:48:41

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

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

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

First argument of imagecopyresampled is resource (image), not string containing file name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文