automatic resize on upload of images

Permalink 4 users found helpful
Hi, I am trying to find a way to resize all images uploaded to my website through file manager down to a certain pixel size. This needs to work for both multiple and single file uploads.

I have seen these posts:
http://www.concrete5.org/community/forums/customizing_c5/resizing-i...
-only apparently works for single file upload not multiple
-doesn't seem to work for me with a 2mb 3264x2448 image (the standard size my user will be uploading)

http://www.concrete5.org/community/forums/customizing_c5/image_uplo...
-@jgarcia I think this was posted in 2009 and works on the old save method

is there a fix for this?

 
buurvrouw replied on at Permalink Reply
buurvrouw
I'm also looking for a solution to this issue. Did you find an answer?
Ekko replied on at Permalink Reply
Ekko
This can be done by using image magic, and php.

Install image magic to your server, and follow thishttp://www.imagemagick.org/discourse-server/viewtopic.php?f=10&...
buurvrouw replied on at Permalink Reply
buurvrouw
thanks, i'm gonna look into this!
Ekko replied on at Permalink Reply
Ekko
If you need I have it on one of my sites, and you can always poke around if need be.
jordanlev replied on at Permalink Reply
jordanlev
This does not answer the OP's question. Concrete5 already requires the GD library which handles image resizing fine -- there is absolutely no reason to install ImageMagick on your server.
The problem is not how to resize the images, but rather how to trigger a resize automatically every time a file is uploaded. For this you might want to see if there is a system event for uploaded files, then use the "create" function in the built-in image helper to do the resizing, like so
$ih = Loader::helper('image');
$ih->create('original/path.jpg', 'new/path.jpg', 500, 300);
Ekko replied on at Permalink Reply
Ekko
Except that wont resize on multiple upload. This was covered thoroughly many times, and image magick was the solution. There are also issues with c5 creating thumbnails with your approach, and again image magick takes care of that as well.
jordanlev replied on at Permalink Reply
jordanlev
Okay, sounds like you have some experience doing this (I haven't seen it covered anywhere, but maybe I don't know where to look... do you have any pointers to where this is discussed?).
Can you explain how to take the ImageMagick code you linked to and integrate that with a C5 site? I'd be very interested to know how to do this.

Thanks!
Ekko replied on at Permalink Reply
Ekko
It was discussed on IRC with a few core members prior to the new 5.5

The issue was that most attempts to hack it in didn't take, and in some cases the ones that did wouldn't create thumbnails of images in the file manager, in others it would only resize the first image in multiple upload.

http://www.concrete5.org/community/forums/customizing_c5/image_uplo...

There's a few more like that thread, and plenty of hacks offered up along the way

The solution I used with heaps of help from Andrew was modified from this
provided by Jason Stone

http://www.concrete5.org/community/forums/usage/upload-image-proble...

""I copied concrete/libraries/file/importer.php to
libraries/file/importer.php and added the following under $fv = File::add($filename, $prefix); (around line ~137):

$file_info = getimagesize($fv->getPath());
if( (($file_info[2] == 1) || ($file_info[2] == 2) || ($file_info[2] == 3) )&&($file_info[0] > 800 || $file_info[1] > 600) ){
exec("convert '".$fv->getPath()."' -resize 800x600 '".$fv->getPath()."'");
}

you also have to copy this 4 lines a second time and place it under:

$fv = $fr->getVersionToModify(true);

in (~line 143), so if you use the "replace" on a file the resizing works here too.

Your image will be resized right after uploading trough ImageMagick to max width/height 800x600 - in my case nobody needs any higher resolution, in fact higher resolution is unwelcome.

1-3 stands for gif,jpg,png.

Now my editors can upload images straight out of digital cameras in single and multi uploads and don't have to worry about anything. Thumbnail gets always created, Upload never seems to be stuck and they don't have to fight with over sized images.

Hope that helps somebody. ""

I have a client site running this now you can always take a look at it if your interested.
jordanlev replied on at Permalink Reply
jordanlev
Wow, this is really useful -- thank you so much for taking the time to spell it all out (especially the c5 integration)!
slurp56 replied on at Permalink Reply 1 Attachment
Ekko,

I appreciate your response, it got me started on my solution.

I am working on a website that is basically an online store, but with the shopping cart component disabled. The client can upload photos of any size and have concrete5 resize them... (therein lies the trick on such a simple task).

I do not have image magik on my server and I doubt the shared host will install it. So I'm using the image helper in concrete5

Here is what my code looks like:

$fv = File::add($filename, $prefix);
$file_info = getimagesize($fv->getPath());
if( (($file_info[2] == 1) || ($file_info[2] == 2) || ($file_info[2] == 3) )&&($file_info[0] > 800 || $file_info[1] > 600) ) {
$hi = Loader::helper('image');
$hi->create($fv->getPath(), $fv->getPath(), 800, 600);
}

I've also attached a modified copy of importer.php which goes in /libraries/file/importer.php
TheRealSean replied on at Permalink Reply
TheRealSean
Great work, thanks very much for working through this.

Image GD chokes on 3k plus wide images so being able to use ImageMagick is something I have wanted to do for a long time but not looked into.

Regards
Sean
popscomp replied on at Permalink Reply
Ekko,

ImageMagick is already installed on my servers.

I've been trying to follow your instructions..but on Concrete5 5.6.0.2, the only contents of the '/libraries/file/importer.php' were

`<?php

defined('C5_EXECUTE') or die("Access Denied.");

class FileImporter extends Concrete5_Library_FileImporter {}`

I was actually able to track down the lines you mentioned in your post to '/concrete/core/libraries/file_importer.php'

After appending the code to reflect the changes you mentioned, what would I do with the new version of file_importer.php?


Thanks!
mhawke replied on at Permalink Reply
mhawke
I believe it goes in [root]/libraries
popscomp replied on at Permalink Reply
Thank you for the speedy reply.

Would I rename it importer.php? Or would I leave it as file_importer.php?
mhawke replied on at Permalink Reply
mhawke
Whatever the core file name that you modified. Whenever you want to make changes to core files, just save them in an identical path off your root instead of the '[root]/concrete/core' folder. For example, if the core file that you are modifying (in version 5.6) was '[root]/concrete/core/libraries/file_importer.php' then your modified file would need to go in '[root]/libraries/file_importer.php'

Capiche?
popscomp replied on at Permalink Reply
Yes, thank you.

I was just unsure, since I found this code under /concrete/core/libraries and there's no folder off my root as /core/libraries, but I'll just create the /core/ directory off my root.

Thanks again for your help.
mhawke replied on at Permalink Reply
mhawke
Nope, just put it in [root]/libraries. No 'core' needed. I modified my instructions above.
popscomp replied on at Permalink Reply 1 Attachment
So, I've been going over this for hours, and I can't seem to get this to work.

As I've said earlier, with concrete5 5.6.0.2, the importer.php contains only:

<?php 
defined('C5_EXECUTE') or die("Access Denied.");
class FileImporter extends Concrete5_Library_FileImporter {}


but, I was actually able to trace those scripts mentioned earlier to /concrete/core/libraries/file_importer.php, I've copied that, modified it, and placed it in [root]/libraries/file_importer.php

I've copied the additional code provided by Ekko:

$file_info = getimagesize($fv->getPath());
if( (($file_info[2] == 1) || ($file_info[2] == 2) || ($file_info[2] == 3) )&&($file_info[0] > 800 || $file_info[1] > 600) ){
    exec("convert '".$fv->getPath()."' -resize 800x600 '".$fv->getPath()."'");
}


and inserted it below $fv = File::add($filename, $prefix); and $fv = $fr->getVersionToModify(true);

and I got nothing.

According to my hosting service, InMotionHosting, they do provide ImageMagick already installed, but following the php script they provide, I still have no luck..http://www.inmotionhosting.com/support/website/general-server-setup...

I've attached the edited file_importer.php if anyone cares to take a peek, the edited section is lines 143-155

Any help is greatly appreciated!
mhawke replied on at Permalink Reply
mhawke
I downloaded the attachment from this post above:

http://www.concrete5.org/community/forums/customizing_c5/automatic-...

and put it in '[root]/libraries/file' and it resizes upon upload.

Just some thoughts on this re-sizing 'feature'. There needs to be a way to control the re-sizing because there may be times when you don't want the system to resize a large. Perhaps this add-on would be a simple and inexpensive long-term option:

http://www.concrete5.org/marketplace/addons/front-end-file-uploader...
popscomp replied on at Permalink Reply
Ahh..

I appreciate your patience mhawke, I finally have it sorted out.

I tried that file earlier, but it didn't seem to work. I thought because of the date, it may have been an older version of concrete, so I was trying to edit Ekko's code.

I went back to the file you recommended, this time cleared my site's cache, and now all files are resized.

Thanks for all your help mhawke.
JohntheFish replied on at Permalink Reply
JohntheFish
A related thing and going way off the original topic, can GD do alpha overlays? (in the past I have used Image Magick to do it). I am looking for an easy semi-transparent stencil solution that I can apply to images from code.
tpwgd replied on at Permalink Reply
Hi everyone - yes i did find a solution to this problem and its probably better than the approach I was first thinking of.

I decided to make use of a package called phpThumb which resizes the image on the fly when it is requested to be displayed rather than when the user uploads their image - this allows you to use the same image uploaded by the user multiple times across the same site at different sizes/resolutions.
arrestingdevelopment replied on at Permalink Reply
arrestingdevelopment
Hi everyone - yes i did find a solution to this problem and its probably better than the approach 
I was first thinking of.
I decided to make use of a package called phpThumb which resizes the image on the fly when it is requested 
to be displayed rather than when the user uploads their image - this allows you to use the same image 
uploaded by the user multiple times across the same site at different sizes/resolutions.

Interesting... and glad that you found something that's working for you, but isn't phpThumb really just the same as using Concrete5's built-in image helper that can generate whatever-sized thumbnail image you want in a block controller or view?

Or am I confused? (wouldn't be the first OR last time, LOL!)

- John
ScottC replied on at Permalink Reply
ScottC
it actually resizes the file in place, the thumbnail helper generates a new thumb and it will be loaded if requested, but it doesn't actually replace the original uploaded file.

That being said i've heard that some shared hosting env have the php exec function disabled so this won't work for everyone, while the thumbnail helper would.
arrestingdevelopment replied on at Permalink Reply
arrestingdevelopment
Scott,

I get that Ekko's solution using ImageMagick is an "on-upload" re-sizer, but the PHPThumb code that @tpwgd was referencing looks like it does the same thing the image helper does.

To ME anyway. ;D

Thanks!

- John
se7en replied on at Permalink Reply
se7en
Its easy enough to write a script to do this if you are willing to get your hands dirty.

a quick search yielded this pagehttp://code.tutsplus.com/tutorials/image-resizing-made-easy-with-ph...

i've not tried this specific tutorial but after reading the intro and having a quick scan it sounds about right.

Just create a directory with a form that allows the user to choose a file/files and on submission runs the scripts -- similar to the one in the link -- and save them to a directory where the user can upload the resized images through the media manager or whatever...

Thats how i've done it -- maybe i'll upload the solution as a plugin?! lol...

Good luck