Image manipulating. How to speed up performance - php

Im using REST API to get products from store, there is around ~3k records. Unfortunately I need create thumbnails of products myself. To do this I use Imagine library for PHP.
After I insert/update products to database in next task I select from database all records and trying to create thumbnail for each of product.
It works... but I'm able to create 12 thumbnails in 120s(thats my execution time script). 12 thumbnails it's far too low for me, I would like to speed up this process, but how can I do this?
All thumbnails are 240x360px, size of each thumbnail is around 12KB.
Here is code which I use for generating thumbnails:
public function generateThumbnails($products)
{
$imagine = new Imagine();
$resize = 240/360;
foreach($products as $product)
{
if(!file_exists('data/thumbs/'.$product['productId'].'.jpg')){
$img = $imagine->open($product['productImage']);
$size = $img->getSize();
$width = $size->getWidth();
$height = $size->getHeight();
$newWidth = floor($height*$resize);
$cordX = $width/2-$newWidth/2;
if($cordX < 0)
$cordX = 0;
$img->crop(new Point($cordX, 0), new Box($newWidth, $height))->resize(new Box(240, 360))->save('data/thumbs/'.$product['productId'].'.jpg');
}
}
}
Im running this script on localhost (Windows 7), it's part of application based on Zend Framework 2 + Doctrine 2

You can use a cache or a file cache when the thumbnail is needed again.

Related

Is there a way of showing a default image if there is no image uploaded?

I am learning Laravel by building a jobs project. I would like to display a default image/logo without adding a default on the database. I also have an Image resizer responsible for resizing logos.
Here is my controller:
$all_jobs = Job::latest()->paginate(8);
$company_logo = new Image;
// loop through jobs and resize image
foreach ($all_jobs as $key => $jobs) {
$all_jobs[$key]->ngo_logo = $company_logo->get_thumb('logos/'.$jobs->ngo_logo);
}
//dd($jobs->ngo_logo);
$data['jobs'] = $all_jobs;
return view('jobs.index', $data);
How do I get the default image which is in public/main/images/default.jpg to show up on the front end if no logo was uploaded? Thanks.
This can be solved without server side:
<img src="<?php echo "image.jpg"; ?>" onerror="this.src='default.jpg'">

Laravel 5.5 Console TV Bar Chart High Charts multipleDatasets and label

I have a Laravel 5.5 app that used this ConsoleTV Chart Package I used a barchart to visualize my data in a monthly report basis. I have the following query below.
$dt = Carbon::now()->year;
$anRequest = AnalysisRequest::where(DB::raw("(DATE_FORMAT(created_at,'%Y'))"),date('Y'))->orderBy('service_id')
->get();
and in my chart
$barChart = Charts::database($anRequest, 'bar', 'highcharts')
->title("Monthly service request")
->elementLabel("Total requests")
->dimensions(1000, 500)
->responsive(false)
->groupByMonth(date('Y'), true);
This code is working perfectly fine and display all the data in by month but how can I add another dataSets or multipleDataSets instead of just having one sets of data to visualize.
For example I will have another query from the AnalysisRequest but with a specific resources.
Sample code below using multi
The query
$tags_jan = DB::table('tags')->whereYear('created_at', $dt)->whereMonth('created_at', '1')->count();
$tags_feb = DB::table('tags')->whereYear('created_at', $dt)->whereMonth('created_at', '2')->count();
$tags_mar = DB::table('tags')->whereYear('created_at', $dt)->whereMonth('created_at', '3')->count();
// and the list goes on which is bad
The charts using multi
$chart = Charts::multi('bar', 'highcharts')
// Setup the chart settings
->title("Total Reports for this year")
// A dimension of 0 means it will take 100% of the space
->dimensions(0, 400) // Width x Height
// This defines a preset of colors already done:)
// ->template("material")
// ->responsive(true)
// You could always set them manually
// ->colors(['#2196F3', '#F44336', '#FFC107'])
// Setup the diferent datasets (this is a multi chart)
->colors(['green', 'aqua', 'red', 'yellow'])
->dataset('Ads', [$ads_jan,$ads_feb,$ads_mar,$ads_apr,$ads_may,$ads_june,$ads_july,$ads_aug,$ads_sept,$ads_oct,$ads_nov,$ads_dec])
->dataset('Channels', [$channels_jan,$channels_feb,$channels_mar,$channels_apr,$channels_may,$channels_june,$channels_july,$channels_aug,$channels_sept,$channels_oct,$channels_nov,$channels_dec])
->dataset('Shows', [$shows_jan,$shows_feb,$shows_mar,$shows_apr,$shows_may,$shows_june,$shows_july,$shows_aug,$shows_sept,$shows_oct,$shows_nov,$shows_dec])
->dataset('Tags', [$tags_jan,$tags_feb,$tags_mar,$tags_apr,$tags_may,$tags_june,$tags_july,$tags_aug,$tags_sept,$tags_oct,$tags_nov,$tags_dec])
This code above also works fine but as you can see in the variable I am querying for each month for this year which is isn't quite good. How can I simplified this query or another way to solve this using multiple dataSets?
Appreciate if someone could help.
Thanks in advance.
The following code can be added after you have queried your database and you would like to post different graphs to the same view, I was using the code to plot a barchart and line chart on the same graph as specified below........ look at the dataset position 2 its line and bar
$chart = new unclaimed;
$chart->labels( $days );
$chart->dataset('Daily Visitors Bar', 'bar', $totalNumber )->color('white')->backgroundColor(['#009900','#8a8a5c','#f1c40f','#e67e22','#16a085','#2980b9']);
$chart->dataset('Daily Visitors Line', 'line', $totalNumber )->color('black') ->fill(false)->backgroundColor(['##8a8a5c','009900','#f1c40f','#e67e22','#16a085','#2980b9']);

Use Product Image as category Image in Virtuemart

I m making a website using Joomla (2.5) and Virtuemart (2.6.14) . My question is :
Is it possible to fill category view thumbnails with a thumbnail of a product that belongs to this category?
I dont want to upload photos to categories one by one because this would take huge amount of time.
Thank you.
You have to create a template override of /components/com_virtuemart/sublayouts/categories.php to /templates/your_template/html/com_virtuemart/sublayouts/categories.php.
And replace this code (approx. line: 74):
echo $category->images[0]->displayMediaThumb("",false);
With:
$productModel = VmModel::getModel('product');
$prod_in_category = $productModel->getProductListing(false, 1, false, true, true, true, $category->virtuemart_category_id);
$productModel->addImages($prod_in_category[0],1);
if(!empty($prod_in_category[0]->images[0])){
echo $prod_in_category[0]->images[0]->displayMediaThumb("",false);
} else {
echo $category->images[0]->displayMediaThumb("",false);
}
You could also use this method that takes a random image but it will take much more memory as it uses an array of all category products.
$productModel = VmModel::getModel('product');
$prod_in_category = $productModel->getProductsInCategory($category->virtuemart_category_id);
$sel = array_rand($prod_in_category);
$productModel->addImages($prod_in_category[$sel],1);
if(!empty($prod_in_category[$sel]->images[0])){
echo $prod_in_category[$sel]->images[0]->displayMediaThumb("",false);
} else {
echo $category->images[0]->displayMediaThumb("",false);
}

Laravel 4 Intervention package gets pixelated images when use fit() method

I'm trying to resize an image using the method fit, and although I get the correct proportions, the result has an awful pixelated effect. How I can prevent this?
if ($imgHeight >= $imgWidth) {
$img->fit((int) ($value[0] * $ratio), $value[0]);
} else {
$img->fit($value[0], (int) ($value[0] / $ratio));
}
The original image is:
https://www.dropbox.com/s/gs7z4s4jg1x6k6o/gallery-portraits-of-strangers-18.jpg?dl=0
And I take this:
https://www.dropbox.com/s/q9l17sf3r5x02fn/63_big.jpg?dl=0
Sorry, I don't have enough reputation to post pics.

Set Base Image Programmatically

I've got this problem that I can't solve. Partly because I can't explain it with the right terms. I'm new to this so sorry for this clumsy question.
Below you can see an overview of my goal.
I am trying to sell Similar product in my magento for this i wrote some code that every thing is working fine..
But i have only one problem with images..
that is I am getting all images of current product but the base image is not selected, How can i set the very first image as base image Programmatically.
Any ideas ?
Hello You can do as follows :
$image =$imagePath."image.png";
$product->setMediaGallery(array('images'=>array (), 'values'=>array ()));
if(is_file($image))
{
$product->addImageToMediaGallery($image, array ('image', 'small_image', 'thumbnail'), false, false);
}
i.e you need to first set the media gallery, P.S This is a necessary step.
then add all images to gallery using addImageToMediaGallery where 'image' ref to 'base_image'
i.e in above example we are setting image.png to base_image, small_image and thumbnail image in a single call.
hope this helps you.
I've achieved the same result by using:
$product->setSmallImage($path)
->setThumbnail($path)
->setImage($path)
->save();
Works better for the case where your media gallery has one or more pictures in it.
I'm doing
$product->load();
$gallery = $product->getMediaGalleryImages();
$paths = array();
foreach ($gallery as $image) {
$paths[] = $image->getFile();
}
sort($paths);
$path = array_shift($paths);
try {
$product->setSmallImage($path)
->setThumbnail($path)
->setImage($path)
->save();
} catch (Exception $e) {
echo $e->getMessage();
}
Which gets all the product images, sort's them by file name and sets the first to the product primary image. As I ran a import and added all my pictures, but didn't set the primary image.
To grab a set or "broken" products I used:
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter('small_image', array('eq' => ''));

Categories