/** * Resize of image uploaded * @param string $source * @param string $dest * @param integer $new_width * @param integer $new_height * @return boolean */ private function resizeImage($source, $dest, $new_width, $new_height) { $image = new GD($source); $source_height = $image->getHeight(); $source_width = $image->getWidth(); $source_aspect_ratio = $source_width / $source_height; $desired_aspect_ratio = $new_width / $new_height; if ($source_aspect_ratio > $desired_aspect_ratio) { $temp_height = $new_height; $temp_width = (int) ($new_height * $source_aspect_ratio); } else { $temp_width = $new_width; $temp_height = (int) ($new_width / $source_aspect_ratio); } $x0 = ($temp_width - $new_width) / 2; $y0 = ($temp_height - $new_height) / 2; $image->resize($temp_width, $temp_height)->crop($new_width, $new_height, $x0, $y0); return $image->save($dest); }
/** * Start Import Product Queue action. * * @return void */ public function importAction($param1 = null, $param2 = null) { $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); $queue = $this->getDI()->get('queue'); $queue->watch('haraapp.import'); $config = $this->getDI()->get('config'); $filefive = $this->getDI()->get('filefive'); while ($job = $queue->reserve()) { $message = $job->getBody(); $data = $message[0]; // Get offline product data from mysql db $myProductQueue = ProductQueue::findFirst(['pid = :haravanProductId: AND status = :status: AND sid = :storeId:', 'bind' => ['haravanProductId' => $data['haravanProductId'], 'status' => ProductQueue::STATUS_QUEUE, 'storeId' => $data['storeId']]]); if ($myProductQueue) { $pass = false; $myStore = Store::findFirstById($data['storeId']); // get content, image and import to five.vn db $product = json_decode($myProductQueue->pdata); $cleanData = strip_tags($product->body_html); // insert table ADS $myAds = new Ads(); $myAds->assign(['uid' => $myStore->uid, 'udid' => "", 'rid' => $product->id, 'cid' => $myProductQueue->fcid, 'title' => $product->title, 'slug' => Utils::slug($product->title), 'description' => $cleanData, 'price' => $product->variants[0]->price, 'instock' => 1, 'cityid' => 0, 'districtid' => 0, 'status' => 1, 'isdeleted' => 0, 'seokeyword' => $product->tags, 'lastpostdate' => time()]); if ($myAds->create()) { $pass = true; if (isset($product->images)) { // Insert table IMAGES foreach ($product->images as $img) { $response = \Requests::get($img->src); if ($response->status_code == 200) { // Download image to local $filePart = explode('.', $img->filename); $namePart = $filePart[0]; $extPart = $filePart[1]; $path = rtrim($config->global->product->directory, '/\\') . '/' . date('Y') . '/' . date('m') . DIRECTORY_SEPARATOR; $fullPath = $config->global->staticFive . $path; $uploadOK = $filefive->put($path . $namePart . '.' . $extPart, (string) $response->body); // Resise image $myResize = new PhImage($fullPath . $namePart . '.' . $extPart); $orig_width = $myResize->getWidth(); $orig_height = $myResize->getHeight(); $height = $orig_height * 1200 / $orig_width; $mediumHeight = $orig_height * 600 / $orig_width; $smallHeight = $orig_height * 200 / $orig_width; $myResize->resize(1200, $height)->crop(1200, $height)->save($fullPath . $namePart . '.' . $extPart); $myResize->resize(600, $mediumHeight)->crop(600, $mediumHeight)->save($fullPath . $namePart . '-medium' . '.' . $extPart); $myResize->resize(200, $smallHeight)->crop(200, $smallHeight)->save($fullPath . $namePart . '-small' . '.' . $extPart); if ($uploadOK) { // Save to db $myImage = new Images(); $myImage->assign(['aid' => $myAds->id, 'name' => $myAds->title, 'path' => $path . $namePart . '.' . $extPart, 'status' => Images::STATUS_ENABLE, 'orderNo' => $img->position]); if ($myImage->save()) { // Update first image to ads table if ($img->position == 1) { $myAds->image = $path . $namePart . '.' . $extPart; $myAds->update(); } } else { echo "cannot save image!"; } } else { echo "cannot download image!"; } } else { echo "cannot get image url!"; } } } $imageName = strlen($myAds->image) > 0 ? $myAds->image : ""; // Save to product_map table $myProduct = new ProductMap(); $myProduct->assign(['sid' => $myStore->id, 'uid' => $myStore->uid, 'hid' => $data['haravanProductId'], 'aid' => $myAds->id, 'cid' => $myAds->cid, 'title' => $myAds->title, 'price' => $myAds->price, 'image' => $imageName, 'slug' => $myAds->slug, 'status' => $myAds->status]); $myProduct->create(); // Delete queued data. (Production) // $myProductQueue->delete(); } else { $pass = false; } if ($pass) { $myProductLog = new ProductLog(); $myProductLog->assign(['sid' => $myStore->id, 'message' => 'Ads name ' . $myAds->title . ' has been created!', 'type' => ProductLog::TYPE_IMPORT, 'status' => ProductLog::STATUS_COMPLETED, 'class' => 'succcess']); $myProductLog->create(); // update okie $myCategoryMap = CategoryMap::findFirst(['hid = :haravanId: AND fid = :fid: AND sid = :storeId:', 'bind' => ['haravanId' => $data['haravanId'], 'fid' => $myProductQueue->fcid, 'storeId' => $data['storeId']]]); $myCategoryMap->totalItemSync++; $myCategoryMap->totalItemQueue--; $myCategoryMap->update(); // generate total process, when import a product success $totalItem = CategoryMap::sum(['column' => 'totalItem', 'conditions' => 'hid = ' . $data['haravanId'] . ' AND sid = ' . $data['storeId']]); $totalItemSync = CategoryMap::sum(['column' => 'totalItemSync', 'conditions' => 'hid = ' . $data['haravanId'] . ' AND sid = ' . $data['storeId']]); $process = $totalItemSync * 100 / $totalItem; // Push process $meta = ['shopName' => $myStore->name, 'record' => $process]; $redis->publish('notification', json_encode($meta)); // send message. // When process all products in category map, update store mapped to OK if ($process == 100) { $myStore->mapped = Store::MAPPED; $myStore->update(); $myProductLog = ProductLog::findFirst('status = ' . ProductLog::STATUS_CURRENT_PROCESSING . ' AND type = ' . ProductLog::TYPE_IMPORT . ' AND sid = ' . $myStore->id); $myProductLog->status = ProductLog::STATUS_COMPLETED; $myProductLog->update(); } } } $job->delete(); } }
public function viewAction() { $this->view->disable(); // echo $this->params->filename; $image = new Phalcon\Image\Adapter\GD($this->params->filename); $realWidth = $image->getWidth(); $realHeight = $image->getHeight(); // $image->resize(100, 100)->rotate(90)->crop(100, 100); // echo $realWidth . "x". $realHeight; $cropSize = $realWidth; if ($realWidth > $realHeight) { $cropSize = $realHeight; } // echo $cropSize; if (!isset($this->params->width)) { $final_width = $realWidth; } else { $final_width = $this->params->width; } if (!isset($this->params->height)) { $final_height = $realHeight; } else { $final_height = $this->params->height; } if (!isset($this->params->crop_mode)) { $crop_mode = "default"; } else { $crop_mode = $this->params->crop_mode; } if (!isset($this->params->crop) and $crop_mode != "default") { $final_width = 100; $final_height = 100; } elseif (isset($this->params->crop) and $crop_mode != "default") { $final_width = $this->params->crop; $final_height = $this->params->crop; } else { } switch ($crop_mode) { case 'center': $offset_x = $final_width / 4; $offset_y = $final_height / 4; break; case 'left': $offset_x = 0; $offset_y = $final_width / 4; break; case 'right': $offset_x = $final_width / 2; $offset_y = $final_height / 4; break; case 'top': $offset_x = 0; $offset_y = $final_height / 4; break; case 'bottom': $offset_x = $final_width / 2; $offset_y = $final_height / 4; break; case 'top-left': $offset_x = 0; $offset_y = 0; break; case 'top-right': $offset_x = $final_width / 2; $offset_y = 0; break; case 'bottom-left': $offset_x = $final_width / 2; $offset_y = 0; break; case 'bottom-right': $offset_x = $final_width / 2; $offset_y = $final_height / 2; break; default: $offset_x = 0; $offset_y = 0; break; } if (!isset($this->params->mode)) { $mode = "preview"; } else { $mode = $this->params->mode; } if ($mode == "thumbnail") { $image->crop($cropSize, $cropSize, $offset_x, $offset_y)->resize($final_width, $final_height); } $this->response->setContentType('image/jpeg'); echo $image->render(); // echo "CROP MODE: ".$crop_mode."=>".$offset_x ."x". $offset_y. " - ".$final_width."x".$final_height; }