⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

create 3 thumbnails from an upload



vnt

vnt
  • profile picture
  • Member

Posted 16 April 2012 - 13:38 PM

After uploading an image, I need to create 3 thumbnails:

1) thumbnails 150 x 200 - filename-small.jpg
2) thumbnails 500px - this with watermark - filename-medium.jpg
3) thumbnails 1024px - filename-large.jpg

4) Original image - filename.jpg

$crud->callback_after_upload(array($this,'create_thumbnails'));



function create_thumbnails($uploader_response,$field_info, $files_to_upload)
{
$this->load->library('image_moo');
//Is only one file uploaded so it ok to use it with $uploader_response[0].
$file_uploaded = $field_info->upload_path.'/'.$uploader_response[0]->name;
$this->image_moo->load($file_uploaded)->resize(800)->save($file_uploaded,true);
return true;
}


Thanks a lot.
D.

xxaxxo

xxaxxo
  • profile picture
  • Member

Posted 17 April 2012 - 05:19 AM

Do you really expect somebody to code it for you ?
That's what I would do if I needed 3 images

1. create a resize helper that uses the CI image manipulation class -> http://codeigniter.com/user_guide/libraries/image_lib.html
l
ets say your helper has 1 function resize which creates 3 resized images with a different name, so now - all you do is:

function create_thumbnails($uploader_response,$field_info, $files_to_upload) {
$this->load->helper('your_helper');
create_resized_images($field_info->upload_path.'/'.$uploader_response[0]->name);
if(file_exists(blabla) {
return TRUE;
} else {
return FALSE;
}

there are pretty good tutorials on how to use the image manipulation class in http://codeigniter.com so ...

vnt

vnt
  • profile picture
  • Member

Posted 17 April 2012 - 07:53 AM

Thanks for your help and tutorials!
Sorry for my English!

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 02 May 2012 - 18:38 PM

Everything that you mentioned for the thumbnails is at the documentation of the image_moo library . It has examples for almost every method. For more you can see:

http://www.matmoo.co...iter/image_moo/

For example:


$file_uploaded = $field_info->upload_path.'/'.$uploader_response[0]->name;
$thumbnail1 = $field_info->upload_path.'/thumb1-'.$uploader_response[0]->name;
$thumbnail2 = $field_info->upload_path.'/thumb2-'.$uploader_response[0]->name;
$thumbnail3 = $field_info->upload_path.'/thumb3-'.$uploader_response[0]->name;

$this->image_moo->load($file_uploaded)->resize(800)->save($file_uploaded,true);
$this->image_moo->load($thumbnail1)->resize_crop(150,200)->save($thumbnail1,true);
$this->image_moo->load($thumbnail2)->load_watermark("filename-medium.jpg")->watermark(5)->resize(400,400)->save($thumbnail2,true);
$this->image_moo->load($thumbnail3)->resize(1024)->save($thumbnail3,true);


If something of these doesn't fit you or it doesn't work read the documentation of the image_moo library and try it by yourself.

Everyone tries to helps you of how to do something but you have to figure out alone your custom solutions.

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 03 May 2012 - 06:17 AM

Aslo [member='vnt'] I think that the image CRUD ( http://www.web-and-d...or-codeigniter/ T ) will help you a lot for this (it is still BETA version but it works)

vnt

vnt
  • profile picture
  • Member

Posted 03 May 2012 - 13:28 PM

[quote]
$file_uploaded = $field_info->upload_path.'/'.$uploader_response[0]->name;
$thumbnail1 = $field_info->upload_path.'/thumb1-'.$uploader_response[0]->name;
$thumbnail2 = $field_info->upload_path.'/thumb2-'.$uploader_response[0]->name;
$thumbnail3 = $field_info->upload_path.'/thumb3-'.$uploader_response[0]->name;

[/quote]
In this example, there is an error on file name:
'/thumb1-'.$uploader_response[0]->name; does not work because it can not rename the file. I do not know how to correct the error.
I solved it by creating three folders:
[quote]
$file_uploaded = $field_info->upload_path.'/'.$uploader_response[0]->name;
$thumbnail1 = $field_info->upload_path.'/thumbs1/'.$uploader_response[0]->name;
$thumbnail2 = $field_info->upload_path.'/thumbs2/'.$uploader_response[0]->name;
$thumbnail3 = $field_info->upload_path.'/thumbs3/'.$uploader_response[0]->name;

[/quote]

thanks a lot!
D.

vnt

vnt
  • profile picture
  • Member

Posted 03 May 2012 - 13:58 PM

[quote]Aslo vnt I think that the image CRUD ( http://www.web-and-d...or-codeigniter/ T ) will help you a lot for this (it is still BETA version but it works)[/quote]
Very interesting!

I have a problem with the upload, the new images are not seen. Images in the folder are 0 kb and thumbnails are not created!
when I delete the new file, I get the following error:
[quote]

[b] A PHP Error was encountered[/b]

Severity: Warning
Message: unlink(assets/uploads/thumb__8c6d-consulenza.jpg) No such file or directory
Filename: libraries/image_crud.php
Line Number: 205

[b] A PHP Error was encountered[/b]

Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /Users/vnt/Sites/vnt/system/core/Exceptions.php:185)
Filename: helpers/url_helper.php
Line Number: 546
[/quote]

PHP Version 5.3.6
D.

mztriz

mztriz
  • profile picture
  • Member

Posted 29 October 2012 - 18:21 PM

Did you figure this out? I'm having a similar issue with image crud where if I upload an image it doesn't upload and all of the other images in the gallery break until refresh.

noskov.biz

noskov.biz
  • profile picture
  • Member

Posted 29 October 2012 - 19:46 PM

Hi, mztriz!
Please, tell me have you any error in your firebug? What the server responces, when you have an error? And one more question what version of image CRUD do you use: 0.5 beta or 0.5?

mztriz

mztriz
  • profile picture
  • Member

Posted 30 October 2012 - 17:03 PM

I am using version .05. This is what happens when I upload a file.


<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message: fopen(tour/15/0ec6-0100010.gif) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: No such file or directory</p>
<p>Filename: libraries/image_crud.php</p>
<p>Line Number: 269</p>


<p>Backtrace: </p>








<p style="margin-left:10px">
File: C:\xampp\htdocs\CampusVirTour\application\libraries\image_crud.php<br />
Line: 269<br />
Function: fopen </p>




<p style="margin-left:10px">
File: C:\xampp\htdocs\CampusVirTour\application\libraries\image_crud.php<br />
Line: 497<br />
Function: _upload_file </p>




<p style="margin-left:10px">
File: C:\xampp\htdocs\CampusVirTour\application\controllers\images.php<br />
Line: 39<br />
Function: render </p>








<p style="margin-left:10px">
File: C:\xampp\htdocs\CampusVirTour\index.php<br />
Line: 270<br />
Function: require_once </p>


</p>


</div>
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message: stream_copy_to_stream() expects parameter 2 to be resource, boolean given</p>
<p>Filename: libraries/image_crud.php</p>
<p>Line Number: 271</p>


<p>Backtrace: </p>








<p style="margin-left:10px">
File: C:\xampp\htdocs\CampusVirTour\application\libraries\image_crud.php<br />
Line: 271<br />
Function: stream_copy_to_stream </p>




<p style="margin-left:10px">
File: C:\xampp\htdocs\CampusVirTour\application\libraries\image_crud.php<br />
Line: 497<br />
Function: _upload_file </p>




<p style="margin-left:10px">
File: C:\xampp\htdocs\CampusVirTour\application\controllers\images.php<br />
Line: 39<br />
Function: render </p>








<p style="margin-left:10px">
File: C:\xampp\htdocs\CampusVirTour\index.php<br />
Line: 270<br />
Function: require_once </p>


</p>


</div>
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message: fclose() expects parameter 1 to be resource, boolean given</p>
<p>Filename: libraries/image_crud.php</p>
<p>Line Number: 272</p>


<p>Backtrace: </p>








<p style="margin-left:10px">
File: C:\xampp\htdocs\CampusVirTour\application\libraries\image_crud.php<br />
Line: 272<br />
Function: fclose </p>




<p style="margin-left:10px">
File: C:\xampp\htdocs\CampusVirTour\application\libraries\image_crud.php<br />
Line: 497<br />
Function: _upload_file </p>




<p style="margin-left:10px">
File: C:\xampp\htdocs\CampusVirTour\application\controllers\images.php<br />
Line: 39<br />
Function: render </p>








<p style="margin-left:10px">
File: C:\xampp\htdocs\CampusVirTour\index.php<br />
Line: 270<br />
Function: require_once </p>


</p>


</div>
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message: getimagesize(tour/15/0ec6-0100010.gif) [<a href='function.getimagesize'>function.getimagesize</a>]: failed to open stream: No such file or directory</p>
<p>Filename: libraries/image_crud.php</p>
<p>Line Number: 275</p>


<p>Backtrace: </p>








<p style="margin-left:10px">
File: C:\xampp\htdocs\CampusVirTour\application\libraries\image_crud.php<br />
Line: 275<br />
Function: getimagesize </p>




<p style="margin-left:10px">
File: C:\xampp\htdocs\CampusVirTour\application\libraries\image_crud.php<br />
Line: 497<br />
Function: _upload_file </p>




<p style="margin-left:10px">
File: C:\xampp\htdocs\CampusVirTour\application\controllers\images.php<br />
Line: 39<br />
Function: render </p>








<p style="margin-left:10px">
File: C:\xampp\htdocs\CampusVirTour\index.php<br />
Line: 270<br />
Function: require_once </p>


</p>


</div>{"success":true}

victor

victor
  • profile picture
  • Member

Posted 30 October 2012 - 19:07 PM

mztriz, what permissions did you set for the "upload" folder?

mztriz

mztriz
  • profile picture
  • Member

Posted 31 October 2012 - 13:19 PM

The upload folder is 755.

EDIT: I think I see what the problem is,
tour/15/0ec6-0100010.gif
is not the right directory for the image (that directory doesn't exsist).

victor

victor
  • profile picture
  • Member

Posted 31 October 2012 - 14:49 PM

set 777 on this directory

mztriz

mztriz
  • profile picture
  • Member

Posted 31 October 2012 - 20:06 PM

Victor, I really appreciate your help. I've tried 777 on linux and I've tried to do this on windows as well, but I get the same error.

Where is it getting this directory for fopen?

[color=#000000][font=Consolas, 'Lucida Console', monospace][size=3]<p>Severity: Warning</p> <p>Message: fopen(C:\xampp\htdocs\CampusVirTour\tour\15) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: No such file or directory</p> <p>Filename: libraries/image_crud.php</p> <p>Line Number: 269</p> [/size][/font][/color]




I've tried changing the lines in image_crud.php (windows)


protected function _upload_file($path) {
$ci = &get_instance();
$ci->load->helper('url');

$gallery = strtolower((string)$this->ci->uri->segment(4));
$input = fopen(FCPATH . 'tour\\' . $gallery, 'r');

$temp = tmpfile();
$realSize = stream_copy_to_stream($input, $temp);
fclose($input);

$target = fopen($path, "w");
fseek($temp, 0, SEEK_SET);
stream_copy_to_stream($temp, $target);
fclose($target);

/* Resizing to 1024 x 768 if its required */
list($width, $height) = getimagesize($path);
if($width > 1024 || $height > 768)
{
$this->image_moo->load($path)->resize(1024,768)->save($path,true);
}
/* ------------------------------------- */

return true;
}


It still goes to the wrong directory ([color=#880000][size=2][background=rgb(248, 248, 248)]C:\xampp\htdocs\CampusVirTour\tour\15)[/background][/size][/color]. Thanks again.

noskov.biz

noskov.biz
  • profile picture
  • Member

Posted 31 October 2012 - 21:16 PM

Hi guys!
[color=#5A5A5A][font=helvetica, arial, sans-serif][size=4]mztriz, [/size][/font][/color]please, tell me if you make a fresh instalation with CodeIgniter 2.1.3 + image CRUD 0.5 and set 777 permitions to the ./assets/uploads/ folder are the same errors occurred with demo images?

sachin vairagi

sachin vairagi
  • profile picture
  • Member

Posted 14 February 2013 - 18:07 PM

i used

      $crud->callback_after_upload(array($this,'create_thumbnails'));

and here is my create_thumbnails() function

 

 

function create_thumbnails($uploader_response,$field_info, $files_to_upload)
{
    $this->load->library('image_moo');
    //Is only one file uploaded so it ok to use it with $uploader_response[0].
$file_uploaded = $field_info->upload_path.'/'.$uploader_response[0]->name;
$thumbnail1 = $field_info->upload_path.'/thumb1'.$uploader_response[0]->name;
$thumbnail2 = $field_info->upload_path.'/thumb2'.$uploader_response[0]->name;
$thumbnail3 = $field_info->upload_path.'/thumb3'.$uploader_response[0]->name;

$this->image_moo->load($file_uploaded)->resize(400)->save($file_uploaded,true);
$this->image_moo->load($thumbnail1)->resize_crop(150,200)->save($thumbnail1,true);
$this->image_moo->load($thumbnail2)->load_watermark("filename-medium.jpg")->watermark(5)->resize(400,400)->save($thumbnail2,true);
$this->image_moo->load($thumbnail3)->resize(1024)->save($thumbnail3,true);
}

 

 

it is creating image of 400x300 in assets\uploads\files directory but not creating thumbnail1,thumbnail2 and thumbnail3

 

can anyone tell me what is going wrong with this code?

 

thanks !
 


sachin vairagi

sachin vairagi
  • profile picture
  • Member

Posted 16 February 2013 - 11:25 AM

hi i am using following code to upload image in different size :-

 

function create_thumbnails($uploader_response,$field_info, $files_to_upload)
{
$this->load->library('image_moo');

//Is only one file uploaded so it ok to use it with $uploader_response[0].
$file_uploaded = $field_info->upload_path.'/'.$uploader_response[0]->name;
$thumbnail1 = $field_info->upload_path.'/thumbs1/'.$uploader_response[0]->name;
$thumbnail3 = $field_info->upload_path.'/thumbs3/'.$uploader_response[0]->name;

$this->image_moo->load($file_uploaded)->resize(100,100)->save($file_uploaded,true);
$this->image_moo->load($thumbnail1)->resize_crop(150,200)->save($thumbnail1,true);
$this->image_moo->load($thumbnail3)->resize(1024)->save($thumbnail3,true);

return true;
}    
 

 

 

but i got an error in alert box-  An error has occurred on uploading.

 

 

please help me.


Martin Tonev

Martin Tonev
  • profile picture
  • Member

Posted 03 April 2013 - 07:34 AM

        $file_uploaded = './upload/pics/'.$uploader_response[0]->name; 
        $file_uploaded2 = './upload/pics/thumb-'.$uploader_response[0]->name; 
        
        $this->image_moo->load($file_uploaded)->resize_crop(400,300)->save($file_uploaded2,true);
        $this->image_moo->load($file_uploaded)->resize(800,600)->save($file_uploaded,true);

 

Here is right way , you must load same image , but save it with new name.


Heyder

Heyder
  • profile picture
  • Member

Posted 05 January 2019 - 06:13 AM

error load image 

 

$file_uploaded = $field_info->upload_path.'/'.$uploader_response[0]->name;
$thumbnail1 = $field_info->upload_path.'/thumb1-'.$uploader_response[0]->name;
$thumbnail2 = $field_info->upload_path.'/thumb2-'.$uploader_response[0]->name;
$thumbnail3 = $field_info->upload_path.'/thumb3-'.$uploader_response[0]->name;

$this->image_moo->load($file_uploaded)->resize(800)->save($file_uploaded,true);
$this->image_moo->load($thumbnail1)->resize_crop(150,200)->save($thumbnail1,true);
$this->image_moo->load($thumbnail2)->load_watermark("filename-medium.jpg")->watermark(5)->resize(400,400)->save($thumbnail2,true);
$this->image_moo->load($thumbnail3)->resize(1024)->save($thumbnail3,true);

 

change

 

 

$file_uploaded = $field_info->upload_path.'/'.$uploader_response[0]->name;
$thumbnail1 = $field_info->upload_path.'/thumb1-'.$uploader_response[0]->name;
$thumbnail2 = $field_info->upload_path.'/thumb2-'.$uploader_response[0]->name;
$thumbnail3 = $field_info->upload_path.'/thumb3-'.$uploader_response[0]->name;

$this->image_moo->load($file_uploaded)->resize(800)->save($file_uploaded,true);
$this->image_moo->load($file_uploaded)->resize_crop(150,200)->save($thumbnail1,true);

$this->image_moo->load($file_uploaded)->load_watermark("filename-medium.jpg")->watermark(5)->resize(400,400)->save($thumbnail2,true);

$this->image_moo->load($file_uploaded)->resize(1024)->save($thumbnail3,true);