File: /home/mostafedeg/public_html/erp/controllers/productReduceImageSizeFix.php
<?php
include_once("../public/config.php");
include("../public/include_dao.php");
//Product
require_once('../models/dao/ProductDAO.class.php');
require_once('../models/dto/Product.class.php');
require_once('../models/mysql/ProductMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductMySqlExtDAO.class.php');
//get the do the action
$do = $_GET['do'];
/* ======================
Controller Name :- productController
OPERTATION in Controller
======================== */
//here the global templates
//$smarty->display("header.html");
//here goes the instances and general variables
//Product
$product = new Product();
$productDAO = new ProductMySqlDAO();
$productExt = new ProductMySqlExtDAO();
//
$onlineStoreSetting = new Onlinestoresetting();
$onlineStoreSettingDAO = new OnlinestoresettingMySqlDAO();
$onlineStoreSettingEX = new OnlinestoresettingMySqlExtDAO();
//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
$programSettingExt = new ProgramsettingsMySqlExtDAO();
//check and use the condition that suite this action
//$started_at = microtime(true);
if (empty($do)) {
//here the permssion check
//$started_at = microtime(true);
$Programsettingdata = $ProgramsettingDAO->load(1);
####2-add normal products
$count = 0;
$start = 0;
$limit = 50;
$end = $start + $limit;
$all_products_count = $productExt->getProductsCount('', $joinConditionWithStore);
for ($i = $start; $i <= $all_products_count->productId; $i += $limit) {
$allproduct = $productExt->queryAllOrderedLimitedSimple($start, $limit);
foreach ($allproduct as $value) {
$parcodeString = useSpecializedParcodeDigits($value->productId, $Programsettingdata->specializedParcodeDigits);
if (!empty($value->logo) && $value->logo != "." && $value->logo != "no image") {
reduceImageSize($value->logo, "../views/default/images/product_image/", "../views/default/images/product_image_resized/");
$count++;
}
}
//
$start += $limit;
$end += $limit;
}
// echo 'Cool, that only took ' . (microtime(true) - $started_at) . ' seconds!';
echo "<span style='color:green'> DONE. </span><br/>";
print_r("<pre>");
echo "<div style='border:1px solid green;margin-right:60%'><span style='color:green'> no of products images recreated : " . $count . " </span><br/>";
print_r("</pre>");
}
/* ---------------------------------------------- */
//this way save image ratio of width ,height
function reduceImageSize($imageName, $targetDir, $targetDirDestination) {
global $hosturl_f;
// $fn = $_FILES['logo' . $i]['tmp_name'];
// $size = getimagesize($fn);
// $newDim = getNewWidthHeightAfterReduce($size[0], $size[1]);
// $width = $newDim[0];
// $height = $newDim[1];
if (!file_exists($targetDirDestination . "../")) {
mkdir($targetDirDestination . "../product_image_resized/", 0777, true);
}
$statusMsg = '';
if (!empty($imageName) && $imageName != "." && $imageName != "no image") {
// Path configuration
//$targetDir;$watermarkImagePath//this is comming from user
####get product image
##if its new image upload it instead of file get and put contents use this
// Upload file to the server
// File upload path
$imageName_basename = basename($imageName);
$targetFilePath = $targetDirDestination . $imageName_basename;
list($width, $height, $type, $attr) = getimagesize($targetDir . $imageName_basename);
//$imageSize = (filesize($targetDir . $imageName_basename) / 1024); //in kb
$originalWidth = $width;
$originalHeight = $height;
$newDim = getNewWidthHeightAfterReduce($width, $height);
$width = $newDim[0];
$height = $newDim[1];
//$theImageURL = $hosturl_f . "/views/default/images/product_image/$imageName";//dont work for linux? not sure some times it works sometimes no
$theImageURL = "../views/default/images/product_image/$imageName"; //this one is more guaranteed
$src = imagecreatefromstring(file_get_contents($theImageURL));
$dst = imagecreatetruecolor($width, $height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $originalWidth, $originalHeight);
imagedestroy($src);
imagepng($dst, $targetDirDestination . $imageName); // adjust format as needed
imagedestroy($dst);
}
//return array($statusMsg, $theImageName);
}
function useSpecializedParcodeDigits($productId, $specializedParcodeDigits) {
$preDigits = '';
if (strlen($productId) != $specializedParcodeDigits) {
$noDigitsLeft = $specializedParcodeDigits - strlen($productId);
for ($j = 0; $j < $noDigitsLeft; $j++) {
$preDigits .= '0';
}
}
$newParcode = 'i' . $preDigits . $productId;
return $newParcode
;
}
function getNewWidthHeightAfterReduce($width, $height) {
$ratio = $width / $height; // width/height
if ($ratio > 1) {
$width = 500;
$height = 500 / $ratio;
} else {
$width = 500 * $ratio;
$height = 500;
}
return array($width, $height);
}
?>