Пример #1
0
 /**
  * Resample this image to the new width and height. If the image is set to constrain, then use only
  * either the width or the height present. If both dimensions are the same, and constrain is set to
  * true, this means that the image is the same size as the original file that created it, unless it has
  * been resampled previously.
  * If an output path is provided, write this image to a file, otherwise, just copy it to a new resource
  * and discard the other one.
  */
 function resample($output_path, $quality = 100)
 {
     /*if the width and height are both set, but the image has not been resampled*/
     if ($this->width == $this->info->width() && $this->height == $this->info->height()) {
         $do_resample = false;
         $new_width = $this->width;
         $new_height = $this->height;
     } else {
         if ($this->width && $this->height) {
             $do_resample = true;
             $new_width = $this->width;
             $new_height = $this->height;
             $orig_width = $this->info->width();
             $orig_height = $this->info->height();
         } else {
             if ($this->width && !$this->height) {
                 $do_resample = true;
                 $orig_width = $this->info->width();
                 $orig_height = $this->info->height();
                 $ratio = $this->width / $orig_width;
                 $new_width = $this->width;
                 $new_height = $orig_height * $ratio;
                 //preserve original ratio
             } else {
                 if ($this->height && !$this->width) {
                     $do_resample = true;
                     $orig_width = $this->info->width();
                     $orig_height = $this->info->height();
                     $ratio = $this->height / $orig_height;
                     $new_height = $this->height;
                     $new_width = $orig_width * $ratio;
                     //preserve original ratio
                 }
             }
         }
     }
     if ($do_resample) {
         $write_image = ImageUtility::createTrueColour($new_width, $new_height);
         imagecopyresampled($write_image, $this->res, 0, 0, 0, 0, $new_width, $new_height, $orig_width, $orig_height);
     } else {
         $write_image = $this->res;
     }
     if ($write_image) {
         $this->write($write_image, $output_path);
     }
 }