Note that there are some explanatory texts on larger screens.

plurals
  1. POResizing and cropping in Codeigniter
    text
    copied!<p>Hi I was wondering if you could help me, basically I am using Codeigniter and I want to be able to upload an image and save it to three different folders as three different sizes, however, they must fit the exact dimensions I specify without looking stretched or distorted.</p> <p>This is my controller - if you could help me I would be most grateful.</p> <pre><code>$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'].'/website/uploads/original/'; $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg'; $this-&gt;load-&gt;library('upload'); $this-&gt;upload-&gt;initialize($config); if(!$this-&gt;upload-&gt;do_upload()) { $error = array('error' =&gt; $this-&gt;upload-&gt;display_errors()); $this-&gt;load-&gt;view('submit', $error); } else { $data['upload_data'] = array('upload_data' =&gt; $this-&gt;upload-&gt;data()); $file_name = $this-&gt;upload-&gt;file_name; list($image_width, $image_height) = getimagesize($_SERVER['DOCUMENT_ROOT'].'/website/uploads/original/'.$file_name); // create small size $config['image_library'] = 'GD2'; $config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/website/uploads/original/'.$file_name; $config['new_image'] = $_SERVER['DOCUMENT_ROOT'].'/website/uploads/small/'.$file_name; $config['maintain_ratio'] = TRUE; $config['width'] = 181; $config['height'] = 115; $config['master_dim'] = 'width'; $this-&gt;load-&gt;library('image_lib'); $this-&gt;image_lib-&gt;initialize($config); if($image_width &gt;= $config['width'] AND $image_height &gt;= $config['height']) { if (!$this-&gt;image_lib-&gt;resize()) { echo $this-&gt;image_lib-&gt;display_errors(); } else { if(file_exists($_SERVER['DOCUMENT_ROOT'].'/website/uploads/small/'.$file_name)) { list($image_width, $image_height) = getimagesize($_SERVER['DOCUMENT_ROOT'].'/website/uploads/small/'.$file_name); if($image_height &gt; '115') { $config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/website/uploads/small/'.$file_name; $y_axis = $image_height - 115; $config['y_axis'] = $y_axis; $config['x_axis'] = 181; $this-&gt;image_lib-&gt;initialize($config); if (!$this-&gt;image_lib-&gt;crop()) { echo $this-&gt;image_lib-&gt;display_errors(); } else { echo "cropped"; } } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload