コード例 #1
0
ファイル: Image.php プロジェクト: alphadevx/alpha
 /**
  * Renders the actual binary image using GD library calls.
  *
  *  @since 1.0
  */
 public function renderImage()
 {
     $config = ConfigProvider::getInstance();
     // if scaled, we need to compute the target image size
     if ($this->scale->getBooleanValue() && isset($_COOKIE['screenSize'])) {
         $originalScreenResolution = explode('x', $config->get('sysCMSImagesWidgetScreenResolution'));
         $originalScreenX = $originalScreenResolution[0];
         $originalScreenY = $originalScreenResolution[1];
         $targetScreenResolution = explode('x', $_COOKIE['screenSize']);
         $targetScreenX = $targetScreenResolution[0];
         $targetScreenY = $targetScreenResolution[1];
         // calculate the new units we will scale by
         $xu = $targetScreenX / $originalScreenX;
         $yu = $targetScreenY / $originalScreenY;
         $this->width = new Integer(intval($this->width->getValue() * $xu));
         $this->height = new Integer(intval($this->height->getValue() * $yu));
         // need to update the cache filename as the dimensions have changed
         $this->setFilename();
     }
     // check the image cache first before we proceed
     if ($this->checkCache()) {
         $this->loadCache();
     } else {
         // now get the old image
         switch ($this->sourceType->getValue()) {
             case 'gif':
                 $old_image = imagecreatefromgif($this->source);
                 break;
             case 'jpg':
                 $old_image = imagecreatefromjpeg($this->source);
                 break;
             case 'png':
                 $old_image = imagecreatefrompng($this->source);
                 break;
         }
         if (!$old_image) {
             $im = imagecreatetruecolor($this->width->getValue(), $this->height->getValue());
             $bgc = imagecolorallocate($im, 255, 255, 255);
             $tc = imagecolorallocate($im, 0, 0, 0);
             imagefilledrectangle($im, 0, 0, $this->width->getValue(), $this->height->getValue(), $bgc);
             imagestring($im, 1, 5, 5, "Error loading {$this->source}", $tc);
             if ($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png')) {
                 imagepng($im);
             } else {
                 imagejpeg($im);
             }
             imagedestroy($im);
         } else {
             // the dimensions of the source image
             $oldWidth = imagesx($old_image);
             $oldHeight = imagesy($old_image);
             // now create the new image
             $new_image = imagecreatetruecolor($this->width->getValue(), $this->height->getValue());
             // set a transparent background for PNGs
             if ($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png')) {
                 // Turn off transparency blending (temporarily)
                 imagealphablending($new_image, false);
                 // Create a new transparent color for image
                 $color = imagecolorallocatealpha($new_image, 255, 0, 0, 0);
                 // Completely fill the background of the new image with allocated color.
                 imagefill($new_image, 0, 0, $color);
                 // Restore transparency blending
                 imagesavealpha($new_image, true);
             }
             // copy the old image to the new image (in memory, not the file!)
             imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $this->width->getValue(), $this->height->getValue(), $oldWidth, $oldHeight);
             if ($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png')) {
                 imagepng($new_image);
             } else {
                 imagejpeg($new_image, null, 100 * $this->quality->getValue());
             }
             $this->cache($new_image);
             imagedestroy($old_image);
             imagedestroy($new_image);
         }
     }
 }