/** * Attached event for anytime a product photo is uploaded * @param $event * @return bool */ public function onUploadPhoto($event) { //We were passed these by the CEventPhoto class $blbImage = $event->blbImage; //$image resource $objProduct = $event->objProduct; $intSequence = $event->intSequence; //Check to see if we have an Image record already $criteria = new CDbCriteria(); $criteria->AddCondition("`product_id`=:product_id"); $criteria->AddCondition("`index`=:index"); $criteria->AddCondition("`parent`=`id`"); $criteria->params = array(':index' => $intSequence, ':product_id' => $objProduct->id); $objImage = Images::model()->find($criteria); if (!$objImage instanceof Images) { $objImage = new Images(); } else { $objImage->DeleteImage(); } //Assign width and height of original $objImage->width = imagesx($blbImage); $objImage->height = imagesy($blbImage); //Assign filename this image, actually write the binary file $strImageName = Images::AssignImageName($objProduct, $intSequence); $objImage->SaveImageData($strImageName, $blbImage); $objImage->product_id = $objProduct->id; $objImage->index = $intSequence; //Save image record Yii::trace("saving {$strImageName}", 'application.' . __CLASS__ . "." . __FUNCTION__); if (!$objImage->save()) { Yii::log("Error saving image " . print_r($objImage->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); return false; } $objImage->parent = $objImage->id; //Assign parent to self $objImage->save(); //Update product record with imageid if this is a primary if ($intSequence == 0) { $objProduct->image_id = $objImage->id; if (!$objProduct->save()) { Yii::log("Error updating product " . print_r($objProduct->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); return false; } } $this->createThumbnails($objImage); return true; }
public function actionCreate() { $model = new DatasetAttributes(); $att = Attribute::model()->findByAttributes(array('attribute_name' => Attribute::FUP)); if (!$att) { $att = new Attribute(); $att->attribute_name = Attribute::FUP; $att->definition = ''; $att->save(); } $model->attribute_id = $att->id; $image = new Images(); if (isset($_POST['DatasetAttributes'])) { $args = $_POST['DatasetAttributes']; $exist = DatasetAttributes::model()->findByAttributes(array('dataset_id' => $args['dataset_id'], 'attribute_id' => $att->id)); if ($exist) { $model = $exist; } $model->attributes = $args; $model->value = ''; //$image->attributes = $_POST['Images']; $image->license = "no license"; $image->photographer = "no author"; $image->source = "gigadb"; if ($image->validate()) { $image->save(); } else { Yii::log(print_r($image->getErrors(), true), 'debug'); } if ($image) { $model->image_id = $image->id; } if ($model->validate()) { $model->save(); $this->redirect('/dataset/' . $model->dataset->identifier); } else { Yii::log(print_r($model->getErrors(), true), 'debug'); } } $this->render('create', array('model' => $model, 'image' => $image)); }
/** * 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; } }
/** * Attached event for anytime a product photo is uploaded * @param $event * @return bool */ public function onUploadPhoto($event) { if (!isset($_SERVER['amazon_key'])) { Yii::log("Attempted Cloud transaction but amazon_key not set", 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); return true; } $this->init(); //We were passed these by the CEventPhoto class $objProduct = $event->objProduct; $intSequence = $event->intSequence; //Check to see if we have an Image record already $criteria = new CDbCriteria(); $criteria->AddCondition("`product_id`=:product_id"); $criteria->AddCondition("`index`=:index"); $criteria->AddCondition("`parent`=`id`"); $criteria->params = array(':index' => $intSequence, ':product_id' => $objProduct->id); $objImage = Images::model()->find($criteria); if (!$objImage instanceof Images) { $objImage = new Images(); } $objImage->product_id = $objProduct->id; $objImage->index = $intSequence; //Save image record Yii::trace("saving " . $objImage->strImageName, 'application.' . __CLASS__ . "." . __FUNCTION__); if (!$objImage->save()) { Yii::log("Error saving image " . print_r($objImage->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); return false; } $objImage->parent = $objImage->id; //Assign parent to self $objImage->save(); //Update product record with imageid if this is a primary if ($intSequence == 0) { $objProduct->image_id = $objImage->id; if (!$objProduct->save()) { Yii::log("Error updating product " . print_r($objProduct->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); return false; } } $this->updateCloudId($event, $objImage->id); return true; }