Image not resizing when added programmatically prestashop - prestashop

I am abel to add image programmatically. But images are not getting resized. In other words resized images are not generating. Attaching the code I use:
$image = new Image();
$image_url = 'http://i.imgur.com/0zSw2gl.jpg';
$id_image = Product::getCover($id_product);
$shops = Shop::getShops(true, null, true);
$image->id_product = $id_product;
$image->position = Image::getHighestPosition($id_product) + 1;
$image->cover = true; // or false;
if (($image->validateFields(false, true)) === true &&
($image->validateFieldsLang(false, true)) === true && $image->add())
{
$image->associateTo($shops);
if (!AdminImportControllerExtended::copyImgCustom($id_product, $image->id, $image_url, 'products', false))
{
$image->delete();
}
}

Images should be resized programatically
I use this code
$image = new Image();
$image->id_product = (int)$product->id;
$image->position = Image::getHighestPosition($product->id) + 1;
$image->cover = 1;
if (!$image->add())
exit(Tools::jsonEncode(array('error' => Tools::displayError('Error while creating additional image'))));
else
{
$new_path = $image->getPathForCreation();
ImageManager::resize($image_path, $new_path.'.'.$image->image_format);
$images_types = ImageType::getImagesTypes('products');
foreach ($images_types as $image_type)
{
if (!ImageManager::resize($image_path, $new_path.'-'.Tools::stripslashes($image_type['name']).'.'.
$image->image_format, $image_type['width'], $image_type['height'], $image->image_format))
exit(Tools::jsonEncode(array('error' => Tools::displayError('An error occurred while copying image:').
' '.Tools::stripslashes($image_type['name']))));
}
$image->update();
}

Related

mapbox omnivore cannot parse kml string

Currently, i've getting the string data from the maps.kml to display in laravel blade for simple testing. Based on the official documentation, mapbox can parse the kml string to maps. But it wont displayed.
Controller
...
$excelFile = public_path().'/assets/excel.xlsx';
$kmlfile = public_path(). '/assets/maps.kml';
$kmlContent = file_get_contents($kmlfile);
$kmlLoad = simplexml_load_string($kmlContent);
$kmlJsonFile = $kmlLoad->Document->Folder->Placemark;
// $kmlJson = json_decode($kmlLoad);
$parse = SimpleXLSX::parse($excelFile);
if ($parse) {
$petaSheet = $parse->rows(0, 0); //worksheet index, row limit
foreach($kmlJsonFile as $item)
{
foreach($petaSheet as $peta)
{
if($peta[0] == $item->name)
{
$item->Style->LineStyle->color = $peta[0];
}
}
}
// $monitorSheet = $parse->rows(1, 0);
} else {
$excelRaw = SimpleXLSX::parseError();
}
// $kmlJsonFile = json_encode($kmlJsonFile);
return view('home', [
'peta' => $petaSheet,
'kml' => $kmlContent
]);
Here the js file inside laravel blade
var kmlString = "";
kmlString += `{!! $kml !!}`;
var runLayer = omnivore.kml.parse(kmlString)
.on('ready', function() {
map.fitBounds(runLayer.getBounds());
})
.addTo(map);

Formatting radgrid exporting to PDF

I have a datatable dt, and want to make a PDF file from it. Everything is dynamic and it works but I can't format the layout such as alternate row background etc. Here is my code :
private void MakeGridExportToPDF(string strTitle, DataTable dt)
{
using (RadGrid grid = new RadGrid { AutoGenerateColumns = false, ShowHeader = true, })
{
grid.NeedDataSource += (object sender, GridNeedDataSourceEventArgs e) =>
{
grid.DataSource = dt;
};
// Add columns
dt.Columns.OfType<DataColumn>().ToList().ForEach(col =>
{
grid.MasterTableView.Columns.Add(new GridBoundColumn { DataField = col.ColumnName, HeaderText = col.Caption.ToCamel() });
});
GridExportSettings export = grid.ExportSettings;
export.OpenInNewWindow = true;
export.FileName = strTitle;
export.IgnorePaging = true;
GridPdfSettings pdf = export.Pdf;
pdf.PageHeight = Unit.Parse("210mm"); //
pdf.PageWidth = Unit.Parse(GetPageWidth(grid.MasterTableView.Columns.Count)); //
pdf.DefaultFontFamily = "Arial Unicode MS";
pdf.PageTopMargin = Unit.Parse("45mm");
grid.ItemCreated += (object sender, GridItemEventArgs e) =>
{
GridItem item = e.Item;
if (item is GridDataItem)
{
item.Style["vertical-align"] = "middle";
item.Style["text-align"] = "center";
}
switch (item.ItemType) //Mimic RadGrid appearance for the exported PDF file
{
case GridItemType.Item:
item.Style["background-color"] = "#DFDFDF";
item.Font.Italic = true;
break;
case GridItemType.AlternatingItem:
item.Style["background-color"] = "#FFFFFF";
break;
case GridItemType.Header:
item.Style["background-color"] = "#FFFFFF";
item.Style["Color"] = "#767676";
item.Font.Bold = true;
break;
case GridItemType.Footer:
item.Style["background-color"] = "#FFFFFF";
break;
}
};
this.smCSVFile.Controls.Add(grid);
grid.MasterTableView.ExportToPdf();
bDone = true;
}
}
What am I doing wrong. the ItemCreated is triggered when grid is created first but not when is exported and don't see any changes. Any help would be greatly appreciated.
My problem has been resolved by removing use(RadGrid grid .... and just make the Radgrid = new RadGrid { AutoGenerateColumns = false, ShowHeader = true, }

how to add an image to product in prestashop

i have a pragmatically added product in my code , the product is added to presta correctly but not about its image .
here is some part of my code that i used :
$url= "localhost\prestashop\admin7988\Hydrangeas.jpg" ;
$id_productt = $object->id;
$shops = Shop::getShops(true, null, true);
$image = new Image();
$image->id_product = $id_productt ;
$image->position = Image::getHighestPosition($id_productt) + 1 ;
$image->cover = true; // or false;echo($godyes[$dt][0]['image']);
if (($image->validateFields(false, true)) === true &&
($image->validateFieldsLang(false, true)) === true && $image->add())
{
$image->associateTo($shops);
if (! self::copyImg($id_productt, $image->id, $url, 'products', false))
{
$image->delete();
}
}
but my product have not any image yet
the problem is in copyImg method ...
here is my copyImg function :
function copyImg($id_entity, $id_image = null, $url, $entity = 'products')
{
$tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
$watermark_types = explode(',', Configuration::get('WATERMARK_TYPES'));
switch ($entity)
{
default:
case 'products':
$image_obj = new Image($id_image);
$path = $image_obj->getPathForCreation();
break;
case 'categories':
$path = _PS_CAT_IMG_DIR_.(int)$id_entity;
break;
}
$url = str_replace(' ' , '%20', trim($url));
// Evaluate the memory required to resize the image: if it's too much, you can't resize it.
if (!ImageManager::checkImageMemoryLimit($url))
return false;
// 'file_exists' doesn't work on distant file, and getimagesize make the import slower.
// Just hide the warning, the traitment will be the same.
if (#copy($url, $tmpfile))
{
ImageManager::resize($tmpfile, $path.'.jpg');
$images_types = ImageType::getImagesTypes($entity);
foreach ($images_types as $image_type)
ImageManager::resize($tmpfile, $path.'-'.stripslashes($image_type['name']).'.jpg', $image_type['width'],
$image_type['height']);
if (in_array($image_type['id_image_type'], $watermark_types))
Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity));
}
else
{
unlink($tmpfile);
return false;
}
unlink($tmpfile);
return true;
}
can anybody help me ?
You have 2 issues:
You are passing 5th parameter (with value) to copyImg, while the function does not have such.
Your foreach ($images_types as $image_type) loop must include the Hook as well (add open/close curly braces).
foreach ($images_types as $image_type)
{
ImageManager::resize($tmpfile, $path.'-'.stripslashes($image_type['name']).'.jpg', $image_type['width'], $image_type['height']);
if (in_array($image_type['id_image_type'], $watermark_types))
Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity));
}
You should also check if the product is imported correctly, expecially the "link_rewrite" and if the image is phisically uploaded in the /img/ folder.

How to save data in db

In user table, the data stored in db as well as resize the images in folders.
But the usergallary table same functionality didn't work only image resized in the folders but not stored in db.
public function actionProfileImage($id) {
$userModel = User::model()->findByAttributes(array('id' => $id));
$userGallary = new UserGallary;
$filePrefix = Yii::app()->params['profile']['prefix'];
$targetImagePath = Yii::app()->params['profile']['image']['path'];
$target150Path = Yii::app()->params['profile']['thumb150']['path'];
$target25Path = Yii::app()->params['profile']['thumb25']['path'];
if (isset($_FILES['uploaded_picture'])) {
$handle = new Upload($_FILES['uploaded_picture']);
if ($handle->uploaded) {
$handle->file_name_body_pre = $filePrefix;
$handle->process($targetImagePath);
if ($handle->processed) {
$handle->file_name_body_pre = $filePrefix;
$handle->image_resize = true;
$handle->image_x = 150;
$handle->image_y = 150;
//$handle->image_ratio_y = true;
$handle->image_ratio = true;
$handle->process($target150Path);
if ($handle->processed) {
$handle->file_name_body_pre = $filePrefix;
$handle->image_resize = true;
$handle->image_x = 25;
$handle->image_y = 25;
// $handle->image_ratio_y = true;
$handle->image_ratio = true;
var_dump($$target150Path);exit;
$handle->process($target25Path);
if ($handle->processed) {
$userGallary->uploaded_picture = "{$filePrefix}.{$handle->file_dst_name_ext}";
$userGallary->save();
}
}
$handle->clean();
}
}
}
echo CJSON::encode( arraY('path150' => $target150Path,
'file' => $userGallary->uploaded_picture,
));
}

Codeigniter Multiple file upload and thumbnail creation: only one thumbnail being uploaded

I am able to upload my images ok, I am able to create thumbnails ok, but only the first thumbnail is being uploaded. I checked the error code for the resize function and they all say 'true' (Success). However, if I upload 4 files, only the first is being uploaded to the thumbnail folder. Here is my code:
function _upload_them_images($_FILES, $last_insert_id)
{
$error = '';
// for($i=0; $i<count($_FILES); $i++)
for($i=0; $i<count($_FILES['imagefile']['name']); $i++)
{
$_FILES['userfile']['name'] = $_FILES['imagefile']['name'][$i];
$_FILES['userfile']['type'] = $_FILES['imagefile']['type'][$i];
$_FILES['userfile']['tmp_name'] = $_FILES['imagefile']['tmp_name'][$i];
$_FILES['userfile']['error'] = $_FILES['imagefile']['error'][$i];
$_FILES['userfile']['size'] = $_FILES['imagefile']['size'][$i];
$config['file_name'] = $last_insert_id.'_'.time().rand(1000,9999).$i;
$config['upload_path'] = './images/vehicles/';
$config['allowed_types'] = 'jpg|jpeg|gif|png';
$config['max_size'] = '1000';
$config['overwrite'] = FALSE;
$this->upload->initialize($config);
if($this->upload->do_upload())
{
$upload_result = $this->upload->data();
$rc = $this->_image_name_into_database(
$last_insert_id,
$upload_result['file_name']);
$image_config = array(
'source_image' =>$upload_result['full_path'],
'new_image' => './images/vehicles/thumbs/',
'create_thumb' => TRUE,
'maintain_ratio' => TRUE,
'width' => 75,
'height' => 50
);
$this->load->library('image_lib', $image_config);
$resize_rc = $this->image_lib->resize();
$error += 0;
}
else
{
//if the image was not uploaded successfully, try resizing
$error += 1;
}
}
if($error > 0)
{
return FALSE;
}
else
{
return TRUE;
}
}
Your image library parameters aren't being updated in the loop. When you call $this->load->...., duplicate items are ignored. You should instead re-initialize the image library with:
$this->image_lib->clear();
$this->image_lib->initialize($image_config);
Before each ->resize()