/** * Create thumbnail for image in specified size * @param $objImage * @param $intNewWidth * @param $intNewHeight * @param $intType */ protected function createThumb($objImage, $intNewWidth, $intNewHeight, $intType) { // Verify that the size doesn't already exist in the db (usually the original which // we don't want to overwrite) $objImageThumbnail = Images::LoadByRowidSize($objImage->id, $intType); if (!is_null($objImageThumbnail)) { return; } //Get our original file from Lightspeed $strOriginalFile = $objImage->image_path; $strTempThumbnail = Images::GetImageName($strOriginalFile, $intNewWidth, $intNewHeight, 'temp'); $strNewThumbnail = Images::GetImageName($strOriginalFile, $intNewWidth, $intNewHeight); $strOriginalFileWithPath = Images::GetImagePath($strOriginalFile); $strTempThumbnailWithPath = Images::GetImagePath($strTempThumbnail); $strNewThumbnailWithPath = Images::GetImagePath($strNewThumbnail); $image = Yii::app()->image->load($strOriginalFileWithPath); $quality = _xls_get_conf('IMAGE_QUALITY', '75'); $sharpness = _xls_get_conf('IMAGE_SHARPEN', '20'); if ($sharpness != 0) { $image->resize($intNewWidth, $intNewHeight)->quality($quality)->sharpen($sharpness); } else { $image->resize($intNewWidth, $intNewHeight)->quality($quality); } if (Images::IsWritablePath($strNewThumbnail)) { if (_xls_get_conf('IMAGE_FORMAT', 'jpg') == 'jpg') { $strSaveFunc = 'imagejpeg'; $strLoadFunc = "imagecreatefromjpeg"; } else { $strSaveFunc = 'imagepng'; $strLoadFunc = "imagecreatefrompng"; } $image->save($strNewThumbnailWithPath); //just save normally with no special effects //See if we have a thumbnail record in our Images table, create or update $objThumbImage = Images::model()->findByAttributes(array('width' => $intNewWidth, 'height' => $intNewHeight, 'index' => $objImage->index, 'parent' => $objImage->id, 'product_id' => $objImage->product_id)); if (!$objThumbImage instanceof Images) { $objThumbImage = new Images(); Images::model()->deleteAllByAttributes(array('width' => $intNewWidth, 'height' => $intNewHeight, 'parent' => $objImage->id)); //sanity check to prevent SQL UNIQUE errors } $objThumbImage->image_path = $strNewThumbnail; $objThumbImage->width = $intNewWidth; $objThumbImage->height = $intNewHeight; $objThumbImage->parent = $objImage->id; $objThumbImage->index = $objImage->index; $objThumbImage->product_id = $objImage->product_id; $objThumbImage->save(); } else { Yii::log("Directory permissions error writing " . $strNewThumbnail, 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); } }
/** * ToDo: need to update and make photo processors use a more condensed version of this * Create Thumbnail from Lightspeed original file. Technically to Web Store, any resized copy of the original * whether larger or smaller is considered a "thumbnail". * @param $intNewWidth * @param $intNewHeight * @return bool|Images */ public function CreateThumb($intNewWidth, $intNewHeight) { // Delete previous thumbbnail if ($this->id) { $objImage = Images::LoadByWidthParent($intNewWidth, $this->id); if ($objImage) { $objImage->Delete(); } } //Get our original file from Lightspeed $strOriginalFile = $this->image_path; $strTempThumbnail = Images::GetImageName($strOriginalFile, $intNewWidth, $intNewHeight, 'temp'); $strNewThumbnail = Images::GetImageName($strOriginalFile, $intNewWidth, $intNewHeight); $strOriginalFileWithPath = Images::GetImagePath($strOriginalFile); $strTempThumbnailWithPath = Images::GetImagePath($strTempThumbnail); $strNewThumbnailWithPath = Images::GetImagePath($strNewThumbnail); $image = Yii::app()->image->load($strOriginalFileWithPath); $image->resize($intNewWidth, $intNewHeight)->quality(_xls_get_conf('IMAGE_QUALITY', '75'))->sharpen(_xls_get_conf('IMAGE_SHARPEN', '20')); if (Images::IsWritablePath($strNewThumbnail)) { if (_xls_get_conf('IMAGE_FORMAT', 'jpg') == 'jpg') { $strSaveFunc = 'imagejpeg'; $strLoadFunc = "imagecreatefromjpeg"; } else { $strSaveFunc = 'imagepng'; $strLoadFunc = "imagecreatefrompng"; } $image->save($strTempThumbnailWithPath, false); $src = $strLoadFunc($strTempThumbnailWithPath); //We've saved the resize, so let's load it and resave it centered $dst_file = $strNewThumbnailWithPath; $dst = imagecreatetruecolor($intNewWidth, $intNewHeight); $colorFill = imagecolorallocate($dst, 255, 255, 255); imagefill($dst, 0, 0, $colorFill); if (_xls_get_conf('IMAGE_FORMAT', 'jpg') == 'png') { imagecolortransparent($dst, $colorFill); } $arrOrigSize = getimagesize($strOriginalFileWithPath); $arrSize = Images::CalculateNewSize($arrOrigSize[0], $arrOrigSize[1], $intNewWidth, $intNewHeight); $intStartX = $intNewWidth / 2 - $arrSize[0] / 2; imagecopymerge($dst, $src, $intStartX, 0, 0, 0, $arrSize[0], $arrSize[1], 100); $strSaveFunc($dst, $dst_file); @unlink($strTempThumbnailWithPath); //We save it, then pass back to do a redir immediately //Make sure we don't have an existing record for whatever reason $objNew = Images::LoadByWidthHeightParent($intNewWidth, $intNewHeight, $this->id); if (!$objNew instanceof Images) { $objNew = new Images(); } $objNew->image_path = $strNewThumbnail; $objNew->parent = $this->id; $objNew->width = $intNewWidth; $objNew->height = $intNewHeight; $objNew->index = $this->index; $objNew->product_id = $this->product_id; try { if (!$objNew->save()) { Yii::log("Thumbnail creation error " . print_r($objNew->getErrors()), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); } } catch (Exception $objExc) { Yii::log("Thumbnail creation exception " . $objExc, 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); } return $objNew; } else { Yii::log("Directory permissions error attempting to save " . $strNewThumbnail, 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); return false; } }
public static function resizeImage($imagePath, $intNewWidth, $intNewHeight) { if (strpos($imagePath, 'http') !== false) { return $imagePath; } //Get our original file from LightSpeed $strOriginalFile = $imagePath; $strTempThumbnail = Images::GetImageName($strOriginalFile, $intNewWidth, $intNewHeight, 'temp'); $strNewThumbnail = Images::GetImageName($strOriginalFile, $intNewWidth, $intNewHeight); if (file_exists(Images::GetImagePath($strNewThumbnail))) { return Images::GetImageUri($strNewThumbnail, true); } $strOriginalFileWithPath = Images::GetImagePath($strOriginalFile); $strTempThumbnailWithPath = Images::GetImagePath($strTempThumbnail); $strNewThumbnailWithPath = Images::GetImagePath($strNewThumbnail); $image = Yii::app()->image->load($strOriginalFileWithPath); $image->resize($intNewWidth, $intNewHeight)->quality(_xls_get_conf('IMAGE_QUALITY', '75'))->sharpen(_xls_get_conf('IMAGE_SHARPEN', '20')); if (Images::IsWritablePath($strNewThumbnail)) { if (_xls_get_conf('IMAGE_FORMAT', 'jpg') == 'jpg') { $strSaveFunc = 'imagejpeg'; $strLoadFunc = "imagecreatefromjpeg"; } else { $strSaveFunc = 'imagepng'; $strLoadFunc = "imagecreatefrompng"; } $image->save($strTempThumbnailWithPath, false); try { $src = $strLoadFunc($strTempThumbnailWithPath); //We've saved the resize, so let's load it and resave it centered $dst_file = $strNewThumbnailWithPath; $dst = imagecreatetruecolor($intNewWidth, $intNewHeight); $colorFill = imagecolorallocate($dst, 255, 255, 255); imagefill($dst, 0, 0, $colorFill); if (_xls_get_conf('IMAGE_FORMAT', 'jpg') == 'png') { imagecolortransparent($dst, $colorFill); } $arrOrigSize = getimagesize($strOriginalFileWithPath); $arrSize = Images::CalculateNewSize($arrOrigSize[0], $arrOrigSize[1], $intNewWidth, $intNewHeight); $intStartX = $intNewWidth / 2 - $arrSize[0] / 2; imagecopymerge($dst, $src, $intStartX, 0, 0, 0, $arrSize[0], $arrSize[1], 100); $strSaveFunc($dst, $dst_file); @unlink($strTempThumbnailWithPath); } catch (Exceiption $e) { } return Images::GetImageUri($strNewThumbnail, true); } else { Yii::log("Directory permissions error attempting to save " . $strNewThumbnail, 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); return false; } }