用一种方法创建几个图像

时间:2014-06-05 20:41:20

标签: php

大家好,我的英语不是很好,但我试着解释清楚。

我有Uploader类和创建不同尺寸图像的方法。

问题在于,当我多次调用方法时,总是会创建一个图像 与最后一个方法的维度。 我无法理解我做错了什么。

感谢您的帮助。

我打电话给我这样的方法:

$img->uploadMainImage();
$img->createThumbs()->createThumbs(300, 400)->createThumbs(600, 600);

这是方法: 将主图像上传到temp:

public function uploadMainImage()
{
    if ($this->_imageError == 0) {

        $this->checkImageSize();
        $this->checkImageExtention();
        $this->checkImageDimensions();

        if(empty($this->_errorCollector)) {
            $this->changeImageName()->moveUploadedImage();
        }

    }

    return $this;
}

创建拇指的方法:

public function createThumbs($width = null , $height = null, $folder = null)
{
      if (!is_null($width) && !is_null($height)) {
          $this->_thumbsWidth  = $width;
          $this->_thumbsHeight = $height;
      }


      if ($this->_imageHeight > $this->_imageWidth) {

          $this->_thumbsWidth = ($this->_imageWidth / $this->_imageHeight) * $this->_thumbsHeight;
      } else {
          $this->_thumbsHeight = ($this->_imageHeight / $this->_imageWidth) * $this->_thumbsWidth;
      }

      $tmpImg = imagecreatetruecolor($this->_thumbsWidth, $this->_thumbsHeight);

      switch ($this->_imageType) {

          case "image/jpeg":
          case "image/jpg":


              $src = imagecreatefromjpeg($this->_destinationFolder['main'] .  $this->_imageNewName);
              imagecopyresampled($tmpImg, $src, 0, 0, 0 ,0, $this->_thumbsWidth, $this->_thumbsHeight, $this->_imageWidth, $this->_imageHeight);
              imagejpeg($tmpImg, $this->_destinationFolder['thumbs'] .  $this->_imageNewName, 100);
              imagedestroy($src);
              break;
          case "image/png":

              $src = imagecreatefrompng($this->_destinationFolder['main'] .  $this->_imageNewName);
              imagecopyresampled($tmpImg, $src, 0, 0, 0 ,0, $this->_thumbsWidth, $this->_thumbsHeight, $this->_imageWidth, $this->_imageHeight);
              imagepng($tmpImg, $this->_destinationFolder['thumbs'] .  $this->_imageNewName, 24);
              imagedestroy($src);
              break;

      }

    return $this;
}

1 个答案:

答案 0 :(得分:1)

$this->_imageNewNamecreateThumbs的每次调用中都不是唯一的,因此您只需反复覆盖相同的图像。在createThumbs方法中添加一些逻辑,以便为名称添加唯一性,这样您每次都会获得一个新的图像文件。

示例实施可能是......

$this->_myNewThumbName .= sprintf("%s_%dx%d_thumb", $this->_imageNewName, $this->_imageWidth, $this->imageHeight);

输出:MyCoolSourceImage_400x300_thumb.png

相关问题