/** * 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); } } }
/** * Handles get requests. * * @param Alpha\Util\Http\Request $request * * @return Alpha\Util\Http\Response * * @since 1.0 * * @throws Alpha\Exception\ResourceNotFoundException * @throws Alpha\Exception\ResourceNotAllowedException */ public function doGet($request) { self::$logger->debug('>>doGet(request=[' . var_export($request, true) . '])'); $config = ConfigProvider::getInstance(); $params = $request->getParams(); try { $imgSource = urldecode($params['source']); $imgWidth = $params['width']; $imgHeight = $params['height']; $imgType = $params['type']; $imgQuality = (double) $params['quality']; $imgScale = new Boolean($params['scale']); $imgSecure = new Boolean($params['secure']); } catch (\Exception $e) { self::$logger->error('Required param missing for ImageController controller[' . $e->getMessage() . ']'); throw new ResourceNotFoundException('File not found'); } $modified = filemtime($imgSource); $responseHeaders = array(); $responseHeaders['Last-Modified'] = date('D, d M Y H:i:s', $modified) . ' GMT'; $responseHeaders['Cache-Control'] = 'max-age=1800'; // exit if not modified if ($request->getHeader('If-Modified-Since') != null) { if (strtotime($request->getHeader('If-Modified-Since')) == $modified) { return new Response(304, '', $responseHeaders); } } // handle secure tokens if ($imgSecure->getBooleanValue() && $config->get('cms.images.widget.secure')) { $valid = $this->checkSecurityFields(); // if not valid, just return a blank black image of the same dimensions if (!$valid) { $im = imagecreatetruecolor($imgWidth, $imgHeight); $bgc = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, $imgWidth, $imgHeight, $bgc); if ($imgSource == 'png' && $config->get('cms.images.perserve.png')) { ob_start(); imagepng($im); $body = ob_get_contents(); $contentType = 'image/png'; ob_end_clean(); } else { ob_start(); imagejpeg($im); $body = ob_get_contents(); $contentType = 'image/jpeg'; ob_end_clean(); } imagedestroy($im); self::$logger->warn('The client [' . $request->getUserAgent() . '] was blocked from accessing the file [' . $imgSource . '] due to bad security tokens being provided'); $responseHeaders['Content-Type'] = $contentType; return new Response(200, $body, $responseHeaders); } } try { $image = new Image($imgSource, $imgWidth, $imgHeight, $imgType, $imgQuality, $imgScale->getBooleanValue(), $imgSecure->getBooleanValue()); ob_start(); $image->renderImage(); $body = ob_get_contents(); ob_end_clean(); } catch (IllegalArguementException $e) { self::$logger->error($e->getMessage()); throw new ResourceNotFoundException('File not found'); } self::$logger->debug('<<__doGet'); if ($imgSource == 'png' && $config->get('cms.images.perserve.png')) { $responseHeaders['Content-Type'] = 'image/png'; } else { $responseHeaders['Content-Type'] = 'image/jpeg'; } return new Response(200, $body, $responseHeaders); }