/** * Generates requested image * * @param string $sImageSource image source * @param string $sImageTarget image target * * @return string */ protected function _generateImage($sImageSource, $sImageTarget) { $sPath = false; if (getGdVersion() !== false && $this->_isTargetPathValid($sImageTarget) && ($sImageType = $this->_getImageType())) { // including generator files includeImageUtils(); // in case lock file creation failed should check if another process did not created image yet if ($this->_lock($sImageTarget)) { // extracting image info - size/quality list($iWidth, $iHeight, $iQuality) = $this->_getImageInfo(); switch ($sImageType) { case "png": $sPath = $this->_generatePng($sImageSource, $sImageTarget, $iWidth, $iHeight); break; case "jpeg": $sPath = $this->_generateJpg($sImageSource, $sImageTarget, $iWidth, $iHeight, $iQuality); break; case "gif": $sPath = $this->_generateGif($sImageSource, $sImageTarget, $iWidth, $iHeight); break; } // releasing.. if ($sPath) { $this->_unlock($sImageTarget); } } else { // assuming that image was created by another process $sPath = file_exists($sImageTarget) ? $sImageTarget : false; } } return $sPath; }
/** * Converts a given source image into a target image * * @param string $imageSource File path of the source image * @param string $imageTarget File path of the image to be generated * * @throws StandardException If the path of imageTarget and generated image are not the same * * @return bool|string Return false on failure or file path of the generated image on success */ protected function _generateImage($imageSource, $imageTarget) { $generatedImagePath = false; list($targetWidth, $targetHeight, $targetQuality) = $this->_getImageInfo(); $fileExtensionSource = strtolower(pathinfo($imageSource, PATHINFO_EXTENSION)); $fileExtensionTarget = strtolower(pathinfo($imageTarget, PATHINFO_EXTENSION)); // Do some validation and return false on failure if (!$this->validateGdVersion() || !$this->validateFileExist($imageSource) || !$this->_isTargetPathValid($imageTarget) || !$this->validateImageFileExtension($fileExtensionSource) || !$this->validateImageFileExtension($fileExtensionTarget) || $fileExtensionSource !== $fileExtensionTarget) { return false; } if ($this->validateFileExist($imageTarget)) { list($currentWidth, $currentHeight) = $this->getImageDimensions($imageTarget); if ($currentWidth == $targetWidth && $currentHeight == $targetHeight) { return $imageTarget; } } // including generator files includeImageUtils(); /** * There may be a different process trying to generate this image at the same moment. * Get a lock in order not to write at the same file at the same time. */ if ($this->_lock($imageTarget)) { // extracting image info - size/quality switch ($fileExtensionSource) { case "png": $generatedImagePath = $this->_generatePng($imageSource, $imageTarget, $targetWidth, $targetHeight); break; case "jpeg": case "jpg": $generatedImagePath = $this->_generateJpg($imageSource, $imageTarget, $targetWidth, $targetHeight, $targetQuality); break; case "gif": $generatedImagePath = $this->_generateGif($imageSource, $imageTarget, $targetWidth, $targetHeight); break; } // target must always be unlocked, no matter what the result of the former image generation was. $this->_unlock($imageTarget); } if ($generatedImagePath && $generatedImagePath != $imageTarget) { throw new StandardException('imageTarget path and generatedImage path differ'); } return $generatedImagePath; }