/** * 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__); } }
/** * ORM level methods */ public function DeleteImage() { if ($this->image_path && file_exists(Images::GetImagePath($this->image_path))) { @unlink($this->GetPath()); } $objEvent = new CEventPhoto('Images', 'onDeletePhoto', null, null, null); if (isset($this->ImagesCloud) && isset($this->ImagesCloud[0])) { $objEvent->cloudinary_public_id = $this->ImagesCloud[0]->cloudinary_public_id; } $objEvent->s3_path = $this->image_path; _xls_raise_events('CEventPhoto', $objEvent); }
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; } }
/** * Flushes a DB Table * This gets called during a Reset Store Products for the following tables in sequence: * Product, Category, Tax, TaxCode, TaxStatus, Family, ProductRelated, ProductQtyPricing, Images * * @param string $passkey * @param string $strObj * @return string */ public function db_flush($passkey, $strObj) { if (!$this->check_passkey($passkey)) { return self::FAIL_AUTH; } if (_xls_get_conf('DEBUG_RESET', 0) == 1) { _xls_log("Skipped flush operation due to DEBUG mode"); return self::OK; } if (!class_exists($strObj)) { _xls_log("SOAP ERROR : There is no object type of {$strObj}"); return self::NOT_FOUND; } if (in_array($strObj, array('Cart', 'Configuration', 'ConfigurationType', 'CartType', 'ViewLogType'))) { _xls_log("SOAP ERROR : Objects of type {$strObj} are not allowed for flushing"); return self::UNKNOWN_ERROR; } /** Lightspeed will send commands to flush the following tables Product Category Tax TaxCode TaxStatus Family ProductRelated ProductQtyPricing Images */ Yii::app()->db->createCommand('SET FOREIGN_KEY_CHECKS=0;')->execute(); //For certain tables, we flush related data as well switch ($strObj) { case "Product": //Yii::app()->db->createCommand()->truncateTable('xlsws_product_image_assn'); Yii::app()->db->createCommand()->truncateTable('xlsws_product_category_assn'); Yii::app()->db->createCommand()->truncateTable('xlsws_classes'); Yii::app()->db->createCommand()->truncateTable('xlsws_family'); Yii::app()->db->createCommand()->truncateTable('xlsws_tags'); Yii::app()->db->createCommand()->truncateTable('xlsws_product_tags'); $strTableName = "xlsws_product"; break; case "Category": Yii::app()->db->createCommand()->truncateTable('xlsws_product_category_assn'); $strTableName = "xlsws_category_addl"; //We blank our caching table, not the real table break; case "Tax": $strTableName = "xlsws_tax"; break; case "TaxCode": $strTableName = "xlsws_tax_code"; break; case "TaxStatus": $strTableName = "xlsws_tax_status"; break; case "Family": $strTableName = "xlsws_family"; break; case "ProductRelated": $strTableName = "xlsws_product_related"; break; case "ProductQtyPricing": $strTableName = "xlsws_product_qty_pricing"; break; case "Images": //Because we could have a huge number of Image entries, we need to just use SQL/DAO directly $cmd = Yii::app()->db->createCommand("SELECT image_path FROM xlsws_images WHERE image_path IS NOT NULL AND left(image_path,2)<>'//'"); $dataReader = $cmd->query(); while (($image = $dataReader->read()) !== false) { @unlink(Images::GetImagePath($image['image_path'])); } $cmd = Yii::app()->db->createCommand("SELECT cloudinary_public_id FROM xlsws_images_cloud WHERE cloudinary_public_id IS NOT NULL"); $dataReader = $cmd->query(); while (($image = $dataReader->read()) !== false) { $objEvent = new CEventPhoto('Images', 'onDeletePhoto', null, null, null); $objEvent->cloudinary_public_id = $image['cloudinary_public_id']; _xls_raise_events('CEventPhoto', $objEvent); Yii::app()->db->createCommand("delete FROM xlsws_images_cloud WHERE cloudinary_public_id='" . $image['cloudinary_public_id'] . "'"); } $objEvent = new CEventPhoto('LegacysoapController', 'onFlushTable', null, null, 0); _xls_raise_events('CEventPhoto', $objEvent); $strTableName = "xlsws_images"; break; } //Then truncate the table Yii::app()->db->createCommand()->truncateTable($strTableName); Yii::app()->db->createCommand('SET FOREIGN_KEY_CHECKS=1;')->execute(); return self::OK; }
public function put_Certificate($ProducerId) { try { $requestBody = $this->_app->request->getBody(); $requestObj = new RequestJSON($requestBody); while (true) { $row = $requestObj->getNext(); if ($row == array()) { break; } $BigPictureContent = base64_decode(str_replace(" ", "+", $row['BigPicture'])); $SmallPictureContent = base64_decode(str_replace(" ", "+", $row['SmallPicture'])); // new if (empty($row['CertificateId'])) { $certificate = R::dispense('producercertificate'); $certificate->certficatetime = $row['CertficateTime']; $certificate->producerid = $ProducerId; $certificate->bigcertificatepath = 'NULL'; $certificate->bigcertificateurl = 'NULL'; $certificate->smallcertificatepath = 'NULL'; $certificate->smallcertificateurl = 'NULL'; $certificate->lastmodifiedtime = now(); $id = R::store($certificate); // Store pictures $certificate = R::findOne('producercertificate', 'id=?', array($id)); $imageArray = Images::GetImagePath($id, "producercertificate", "BigCertificate.jpg"); if (!is_dir($imageArray[0])) { mkdir($imageArray[0], 0777, true); } $certificate->bigcertificatepath = $imageArray[0] . $imageArray[1]; $certificate->bigcertificateurl = $imageArray[2]; $myfile = fopen($certificate->bigcertificatepath, "w"); fwrite($myfile, $BigPictureContent); fclose($myfile); $imageArray = Images::GetImagePath($id, "producercertificate", "SmallCertificate.jpg"); if (!is_dir($imageArray[0])) { mkdir($imageArray[0], 0777, true); } $certificate->smallcertificatepath = $imageArray[0] . $imageArray[1]; $certificate->smallcertificateurl = $imageArray[2]; $myfile = fopen($certificate->smallcertificatepath, "w"); fwrite($myfile, $SmallPictureContent); fclose($myfile); $id = R::store($certificate); } else { $certificate = R::findOne('producercertificate', 'id=?', array($row['CertificateId'])); $certificate->certficatetime = $row['CertficateTime']; $certificate->lastmodifiedtime = now(); /*$myfile = fopen($certificate->bigcertificatepath, "w"); fwrite($myfile, $BigPictureContent); fclose($myfile); $myfile = fopen($certificate->smallcertificatepath, "w"); fwrite($myfile, $SmallPictureContent); fclose($myfile); R::store($certificate);*/ // Store pictures $imageArray = Images::GetImagePath($row['CertificateId'], "producercertificate", "BigCertificate.jpg"); if (!is_dir($imageArray[0])) { mkdir($imageArray[0], 0777, true); } $certificate->bigcertificatepath = $imageArray[0] . $imageArray[1]; $certificate->bigcertificateurl = $imageArray[2]; $myfile = fopen($certificate->bigcertificatepath, "w"); fwrite($myfile, $BigPictureContent); fclose($myfile); $imageArray = Images::GetImagePath($row['CertificateId'], "producercertificate", "SmallCertificate.jpg"); if (!is_dir($imageArray[0])) { mkdir($imageArray[0], 0777, true); } $certificate->smallcertificatepath = $imageArray[0] . $imageArray[1]; $certificate->smallcertificateurl = $imageArray[2]; $myfile = fopen($certificate->smallcertificatepath, "w"); fwrite($myfile, $SmallPictureContent); fclose($myfile); R::store($certificate); } } // package response $certs = R::find('producercertificate', 'ProducerId=?', array($ProducerId)); $response = new ResponseJSON(["id", "certficatetime", "bigcertificateurl", "smallcertificateurl"]); foreach ($certs as $cert) { $response->appendData($cert); } //print json_encode($response2->exportResponse())."\n"; echo $response->exportResponse(); } catch (Exception $ex) { return ExceptionHandler::Response($ex, $this->_app); } }
public function put_Images($ProductId) { try { $requestBody = $this->_app->request->getBody(); $requestObj = new RequestJSON($requestBody); while (true) { $row = $requestObj->getNext(); if ($row == array()) { break; } $BigPictureContent = base64_decode(str_replace(" ", "+", $row['BigPicture'])); $SmallPictureContent = base64_decode(str_replace(" ", "+", $row['SmallPicture'])); //new if (empty($row['ImageId'])) { $productimage = R::dispense('productimage'); $productimage->description = $row['Description']; $productimage->productid = $ProductId; $productimage->bigportraitpath = 'NULL'; $productimage->bigportraiturl = 'NULL'; $productimage->smallportraitpath = 'NULL'; $productimage->smallportraiturl = 'NULL'; $productimage->lastmodifiedtime = now(); $id = R::store($productimage); // Store pictures $productimage = R::findOne('productimage', 'id=?', array($id)); $imageArray = Images::GetImagePath($id, "product", "BigProduct.jpg"); if (!is_dir($imageArray[0])) { mkdir($imageArray[0], 0777, true); } $productimage->bigportraitpath = $imageArray[0] . $imageArray[1]; $productimage->bigportraiturl = $imageArray[2]; $myfile = fopen($productimage->bigportraitpath, "w"); fwrite($myfile, $BigPictureContent); fclose($myfile); $imageArray = Images::GetImagePath($id, "product", "SmallProduct.jpg"); if (!is_dir($imageArray[0])) { mkdir($imageArray[0], 0777, true); } $productimage->smallportraitpath = $imageArray[0] . $imageArray[1]; $productimage->smallportraiturl = $imageArray[2]; $myfile = fopen($productimage->smallportraitpath, "w"); fwrite($myfile, $SmallPictureContent); fclose($myfile); $id = R::store($productimage); } else { //echo "aaaaaaa\r\n"; $productimage = R::findOne('productimage', 'id=?', array($row['ImageId'])); $productimage->description = $row['Description']; $productimage->lastmodifiedtime = now(); /*$myfile = fopen($productimage->bigportraitpath, "w"); fwrite($myfile, $BigPictureContent); fclose($myfile); $myfile = fopen($productimage->smallportraitpath, "w"); fwrite($myfile, $SmallPictureContent); fclose($myfile); R::store($productimage);*/ // Store pictures $imageArray = Images::GetImagePath($id, "product", "BigProduct.jpg"); if (!is_dir($imageArray[0])) { mkdir($imageArray[0], 0777, true); } $productimage->bigportraitpath = $imageArray[0] . $imageArray[1]; $productimage->bigportraiturl = $imageArray[2]; $myfile = fopen($productimage->bigportraitpath, "w"); fwrite($myfile, $BigPictureContent); fclose($myfile); $imageArray = Images::GetImagePath($id, "product", "SmallProduct.jpg"); if (!is_dir($imageArray[0])) { mkdir($imageArray[0], 0777, true); } $productimage->smallportraitpath = $imageArray[0] . $imageArray[1]; $productimage->smallportraiturl = $imageArray[2]; $myfile = fopen($productimage->smallportraitpath, "w"); fwrite($myfile, $SmallPictureContent); fclose($myfile); $id = R::store($productimage); } } // package response $images = R::find('productimage', 'productid=?', array($ProductId)); $response = new ResponseJSON(["id", "productid", "description", "bigportraiturl", "smallportraiturl"]); foreach ($images as $image) { $response->appendData($image); } //print json_encode($response2->exportResponse())."\n"; echo $response->exportResponse() . "\n"; } catch (Exception $ex) { return ExceptionHandler::Response($ex, $this->_app); } }
public function put_Portrait_ConsumerId($ConsumerId) { try { //$paramValue = $this->_app->request->getBody(); //$data = json_decode($paramValue); $requestJson = RequestBodyHandler::getJsonBody($this->_app); RequestBodyHandler::verifyJsonBody($requestJson, array("BigPortrait", "SmallPortrait")); $consumer = R::findOne('consumer', 'id=?', array($ConsumerId)); if (!isset($consumer) || empty($consumer)) { throw new RecordNotFoundException("Record not found, id:" . $ConsumerId); } $BigPortraitContent = base64_decode(str_replace(" ", "+", $requestJson->BigPortrait)); $SmallPortraitContent = base64_decode(str_replace(" ", "+", $requestJson->SmallPortrait)); $imageArray = Images::GetImagePath($ConsumerId, "consumer", "BigPortrait.jpg"); if (!is_dir($imageArray[0])) { mkdir($imageArray[0], 0777, true); } $consumer->bigportraitpath = $imageArray[0] . $imageArray[1]; $consumer->bigportraiturl = $imageArray[2]; $myfile = fopen($consumer->bigportraitpath, "w"); fwrite($myfile, $BigPortraitContent); fclose($myfile); /*$imageArray = Images::GetImagePath($ConsumerId, "consumer", "SmallPortrait.jpg"); $consumer->smallportraitpath = $imageArray[0];//"C:\\Apache24\\htdocs\\AppPicture\\" . $ConsumerId . ".ConsumerSmallPortrait.jpg"; $consumer->smallportraiturl = $imageArray[1];//"http://localhost:8080/AppPicture/" . $ConsumerId . ".ConsumerSmallPortrait.jpg"; $myfile = fopen($consumer->smallportraitpath, "w"); fwrite($myfile, $requestJson->SmallPortrait); fclose($myfile);*/ $imageArray = Images::GetImagePath($ConsumerId, "consumer", "SmallPortrait.jpg"); if (!is_dir($imageArray[0])) { mkdir($imageArray[0], 0777, true); } $consumer->smallportraitpath = $imageArray[0] . $imageArray[1]; $consumer->smallportraiturl = $imageArray[2]; $myfile = fopen($consumer->smallportraitpath, "w"); fwrite($myfile, $BigPortraitContent); fclose($myfile); R::store($consumer); $response = R::find('consumer', 'id=?', array($ConsumerId)); //echo json_encode(R::exportAll($response),JSON_UNESCAPED_SLASHES); echo ResponseJsonHandler::normalizeJsonResponse(R::exportAll($response)); } catch (Exception $ex) { return ExceptionHandler::Response($ex, $this->_app); } }