Victorino
Victorino

Reputation: 1641

create several images with one method

hello guys my English is not very good, but i try to explain my self clear.

i got Uploader class and method for creating images with different dimensions.

the problem is that when i'm calling to method couple of times it is always creating one image with dimensions of last method. i cant't understand what i'm doing wrong.

thanks for help.

i'm calling to my method like this :

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

here is the method : upload main image to temp:

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

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

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

    }

    return $this;
}

method for creating thumbs:

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

Upvotes: 1

Views: 32

Answers (1)

phpisuber01
phpisuber01

Reputation: 7715

$this->_imageNewName is not unique on each call of createThumbs, therefore your just overwriting the same image over and over. Add some logic to your createThumbs method to add uniqueness to the name so you will get a new image file each time.

A sample implementation could be...

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

Outputs: MyCoolSourceImage_400x300_thumb.png

Upvotes: 1

Related Questions