Jump to content

via Ad Packs

create 3 thumbnails from an upload


  • Please log in to reply
17 replies to this topic

#1 vnt

vnt

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts

Posted 16 April 2012 - 01: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.

#2 xxaxxo

xxaxxo

    grocery CRUD Expert

  • Members
  • PipPipPip
  • 71 posts
  • LocationBulgaria

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.c...lib.html<br />lets 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 ...

#3 vnt

vnt

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts

Posted 17 April 2012 - 07:53 AM

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

#4 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 916 posts
  • LocationLondon

Posted 02 May 2012 - 06: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.
Posted Image

#5 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 916 posts
  • LocationLondon

Posted 03 May 2012 - 06:17 AM

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)
Posted Image

#6 vnt

vnt

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts

Posted 03 May 2012 - 01:28 PM

$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;

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:

$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;


thanks a lot!
D.

#7 vnt

vnt

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts

Posted 03 May 2012 - 01:58 PM

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)

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:

A PHP Error was encountered

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

A PHP Error was encountered

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


PHP Version 5.3.6
D.

#8 mztriz

mztriz

    Member

  • Members
  • PipPip
  • 17 posts

Posted 29 October 2012 - 06: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.

#9 noskov

noskov

    grocery CRUD Guru

  • Advanced Member
  • PipPipPip
  • 175 posts
  • LocationKiev

Posted 29 October 2012 - 07: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?

#10 mztriz

mztriz

    Member

  • Members
  • PipPip
  • 17 posts

Posted 30 October 2012 - 05: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}


#11 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 946 posts
  • LocationMinsk

Posted 30 October 2012 - 07:07 PM

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

logo.jpg    www.svc.by


#12 mztriz

mztriz

    Member

  • Members
  • PipPip
  • 17 posts

Posted 31 October 2012 - 01: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).

#13 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 946 posts
  • LocationMinsk

Posted 31 October 2012 - 02:49 PM

set 777 on this directory

logo.jpg    www.svc.by


#14 mztriz

mztriz

    Member

  • Members
  • PipPip
  • 17 posts

Posted 31 October 2012 - 08: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 (C:\xampp\htdocs\CampusVirTour\tour\15). Thanks again.

#15 noskov

noskov

    grocery CRUD Guru

  • Advanced Member
  • PipPipPip
  • 175 posts
  • LocationKiev

Posted 31 October 2012 - 09:16 PM

Hi guys!
mztriz, 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?

#16 sachin vairagi

sachin vairagi

    Advanced Member

  • Members
  • PipPipPip
  • 39 posts

Posted 14 February 2013 - 06: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 !
 



#17 sachin vairagi

sachin vairagi

    Advanced Member

  • Members
  • PipPipPip
  • 39 posts

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.



#18 Martin Tonev

Martin Tonev

    Newbie

  • Members
  • Pip
  • 1 posts

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users