generate thumbnail on uploading image using zend framework - php

I am back again with a question in zend framework. Can someone explain me, how to generate thumbnail images in zend framework after uploading an image.
Thanks!

Zend itself seems will not provide this ability http://framework.zend.com/wiki/display/ZFPROP/Zend_Image+-+Eric+Potvin and propose to use http://www.imagemagick.org/script/index.php.
then you are free to use your own way

Not a purely Zend Framework answer, but lately I have found myself cheating like crazy, as follows.
I install phpThumb somewhere inside a web-accessible folder, say:
http://example.com/img/t/phpThumb.php
I make sure to configure phpThumb to use a cache folder:
/path/to/myapp/public/img/t/cache
which I chmod to be web-server writable.
Then I have a phpThumb view-helper that allows me to call an original image with some resize parameters:
<img src="<?php $view->phpThumb($origImgUrl, $desiredWidth, $desiredHeight) ?>">
All the view helper really does is transform the src url into one that runs through my phpThumb installation, making sure to add the cool (!) zoom-center parameter:
<img src="/img/t/phpThumb.php?src=origUrl&w=200&h=150&zc=1">
The first call to this image with these resize params results in a full GD/ImageMagick-driven resize, with the full performance hit. But subsequent calls for that image will pull from cache. Not as good as a request for a truly static image, but usually acceptable for my relatively low-volume purposes.
This is especially useful during design/development, when I am not sure precisely how big I want some front-end, eye-candy image to be. So I can just stash a single, relatively-large verison of my image (say 640x480) out in public/img/someBigImage.png and then resize at will.
It's a hack - probably better to perform this resizing on upload, as you are actually asking - but I must admit I have used it in a few spots. Don't tell anybody. ;-)

Related

What is the best way for creating thumbnail from an existing image

I have an image column in the database contains the images paths of my posts table, I'm using that column in two different places one for a single post page with a width of 800px and height of 400px, in my blog page I wanna use these images but this time with different width and height (thumbnails for posts)..
I have two options and I'm not sure which one is better!
The first one I call a function with the image path, width and height, therefore it returns the optimized version, and to be honest, I didn't try to do this and I don't know what's happening behind the scenes I think it's creating a thumbnail version on the server?
<img src="{{ resizeImage( url('images/posts/' . $post->image) ), '400', '250') }}" />
The second one is when I upload a post image I made a second version for thumbnails and call it like this THUMBNAIL_1562572708.jpg for example.
my question is about performance and best practices of doing this, any recommendations and if there is a better way than that please share it I'm using Laravel, thanks.
I think that the second one is better because we don't need to call a function or processes something when sending the request.
but this way also has a disadvantage which is duplicating files, I am confused, actually :(
ـــــــــــــــــــــــــــــــــــــــ
Well! here is an update with a real example I want to share with anyone who need help.
you will need to install these awesome packages
composer require intervention/image intervention/imagecache
Please read about URL based image manipulation mechanism
http://image.intervention.io/use/url
after publishing the configuration files you will need to edit the imagecache config file like so:
I suppose your images are in the public/images!
so we will read from the public directory.
...
'route' => 'thumbnails',
...
'paths' => array(
// From public
public_path(),
),
...
<img src="{{ 'http://127.0.0.1:8000/your_route_name_that_was_setted_in_imagecache_config_file/your_template_name/images/1.png' }}" />
Notice you have three default templates small, medium, and large, and of course, you can make your own template.
The answers to this question may be highly opinionated, but here are my two cents.
In general, you should go for separate thumbnails that are generated during upload of images or, which is way better, in the background after uploading an image. With Laravels queue system, this is pretty straight forward. The reasons are very simple and already mentioned in the comments:
Thumbnails only need to be generated once (generating thumbnails on a per-request basis causes heavy workload and a potential performance issue)
Storage is cheap and fast
Identication of thumnails can either be done by storing the filename of the thumbnail or, as you already suggested, by using some logic to resolve thumbnails based on the original file. Personally, I would use a folder structure that allows you to generate a wide variety of different thumbnails, e.g.:
images/
original/
123456789.jpg
thumbnail/
100x100/
123456789.jpg
200x200/
123456789.jpg
The directory names 100x100 and 200x200 can of course be changed to something that suits you. Using the suggested ones would mean to me that a thumbnail is not wider than 100px and not higher than 100px either. It doesn't mean every thumbnail is exactly 100x100 pixels, although you can also implement such a thumbnail generator if you need.
Be careful that you might need some fallback logic to display thumbnails while they are still being generated if you utilize the background workers for this. A fallback could be a default image or the original resized via HTML.
By the way, if you implement a thumbnail generator, you can consider implementing an image optimizer as well. Most images can be minified without users noticing a (significant) difference. Whether such a system is worth it or not, depends on the amount of images you display and how much traffic there is on your site. For heavily used websites, this may be a good idea to save some traffic. An existing solution for this is spatie/image-optimizer (I'm not associated with spatie in any way).

Using CDN with Image Resizer Script

I'm using an image resizer to output images in my website. Is it possible to use a CDN in the current situation?
The image resizer takes the file path of the image and outputs the desired image. Now even if I use a CDN, the image resizer script is hosted on my server. So every image request is going through my server. Will using a CDN benefit me in any way?
The cached object on CDNs are keyed on the request URI, so you should benefit from a CDN provided you application isn't generating any randomness in the URLs. If your image request looks like this
/resizer/200x100/thing.jpg
# ...or...
/resizer/thing.jpg?size=200x100
Then the CDN will cache it for all subsequent requests.
If your sever and script is quicker enough then I would use your server code. This means you can play around with the script if you need to add custom functions. If you have any serious problems or want much more options which a CDN may provide then switch.
Short answer: No. If your server resizes images on-the-fly, then it still serves as a bottleneck, and the benefits of the CDN are essentially lost.
Three solutions you might be comfortable with:
Have the image resizer script run once upon image upload, and create all necessary image sizes, then store everything in a logical manner on the CDN. (Reasonable solution. Downside: Very rigid, adding new image sizes requires considerable work).
Have the image resizer script run (ie: resize a single image and upload to the CDN) upon request, but only if the image does not exist on the CDN already. (You can either store a list of created images in a database or, preferably, if at all possible use the object notation default image technique) (Cool solution. Downside: Old browsers don't like the object tag, non-standard, albeit valid, code).
Switch CDNs, use a more mature CDN service that allows you to manipulate media files via API. ex: http://cloudinary.com/ (Smooth sailing solution. Downside: Not as cheap as the non-intelligent CDNs out there, but in most cases you should hardly feel it, and it will save you a ton of coding).
Hope this helps, I'd love to hear what solution you chose.

PHP Thumbnail Class

I am looking for a solid PHP thumbnail generating class. Does anyone know any good ones that are open-source?
I could write one, but I really don't want to. The one thing I hate most about PHP is manipulating images with GD and Imagemagick.
Does anyone have any suggestions?
Use phpThumb(). Its a script that internally uses GD library and/or ImageMagick (whichever is available and whichever it thinks is best for the job) to perform basic image manipulation tasks, including thumbnail generation and square thumbnail generation.
You can use it like this:
<!-- best fit -->
<img src="/phpThumb/phpThumb.php?src=/path/to/image.jpg&w=64&h=64">
<!-- crop fit (square thumbnails) -->
<img src="/phpThumb/phpThumb.php?src=/path/to/image.jpg&w=64&h=64&zc=1">
It has built in caching engine so second time a browser requests the above image it is served from its own cache instead of re-generating the thumbnail every time. Though, you may want to spend an hour or so configuring it.
use class.upload.php
see this link for details may be its help you more
http://www.verot.net/php_class_upload_samples.htm
Generating a thumbnail requires so little code that it is a "simple example" of the GD library's resizing functions in the manual:
http://php.net/manual/en/function.imagecopyresampled.php
Just copy and paste.

In CakePHP, whats the best Image Upload / Resizing component you've used?

Im looking for somthing that can belong to many many models and, hopfully, has all the resizing functionality built in too.
Wondering if there's any good suggestions out there, I don't want to pick the wrong one.
Or is it just worth building my own?
I wrote a pretty sweet image upload component a few years ago for Cake 1. I don't have a clue where the code is these days, but my friend Koa did a write up on it a while back:
http://labs.iamkoa.net/2007/10/23/image-upload-component-cakephp/
http://labs.iamkoa.net/2007/11/06/multiple-image-uploads-into-single-mysql-table-cakephp/
Might be something you can hack. It handles uploads, resizing (resize, crop, resize then crop, max, mix) and if I remember correctly, it might even do filters (flipping, inverse, greyscale).
I also ported the code to Zend framework (and PEAR file naming) for my new content management framework. It's all contained and much better now (does all the things I mentioned above and is pretty well documented). You can download that here (check out the ImageManipulator class. I think the FileUploader class has a dependency on my String utils class which you could easily remove if you want to use that.
Otherwise, I say roll your own. The component architecture in Cake was confusing to me at first so it could be a good learning XP if you don't know it yet. If you do, you can just grab someones uploader and convert it into a component for your Cake application.
Check out the Media plugin by #nperson https://github.com/davidpersson/media/wiki
https://thoughtwm.com/code/ for the image upload
&
http://www.darrenhoyt.com/2008/04/02/timthumb-php-script-released/ for the image resizing
How about for getting EXIF/Meta info of the image?

Merging two Images with CodeIgniter Image Manipulation Class

I am developing a new project in CodeIgniter (CI), and would like to find a CI solution to this current issue. I've got one image, which resembles a picture frame. I would like for uploaded images to be modified to have this frame in their background.
Example:
alt text http://www.sampsonresume.com/projects/visit-creation/vc-process.jpg
The first picture is the frame. The second is the uploaded image (after resizing/cropping), and the third is the combination of both, which I would like to accomplish.
If the images are going to be shown on your own website, it would probably be easier to simply use CSS (especially if your bottom image simply gives a border...). Of course, if you want people to be downloading these files with the frame, that's a different story.
I don't think CI has a built in method in the Image Manipulation class. But, that class is simply abstracts typically-used features from one of the three major PHP image libraries (GD, ImageMagick, and NetPBM). If you know what library you are using (I normally use GD for basic things like this...), then you'll just have to use their proprietary ways of doing this stuff.
You can even make your own library or extend the Image Manipulation class to add your own special features.
Also, there may be ways to do this in Zend (not sure, I'm not going to look it up either). But, there are easy ways to include Zend components into CI if you do find something.
Good luck man!
Agreed with Kyle -- your best bet is to extend the libraries already there. Take a look at ImageMagick "composite" command.
composite -gravity center smile.gif rose: rose-over.png
http://www.imagemagick.org/script/composite.php

Categories