Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeigniter Image Manipulation Class : Resize and Crop on multiple files
    primarykey
    data
    text
    <p>I'm struggling to get the CodeIgniter Image Manipulation working correctly. Either it's a bug or I'm just not seeing it. I hope someone can help me with it. Thanks in advance!</p> <p>On the script: I want to create thumbnails (176w x 132h). The input images are in different sizes and ratios. In order to always get this size I first resize them to fit the max width or height (depending on image orientation) and then crop in the center. I've tried to do it all in 1 method. That didn't work, so I created two seperate methods.</p> <pre><code>resize_img() </code></pre> <p>and</p> <pre><code>crop_img(). </code></pre> <p>When I run resize_img() on 3 different files, it works. If after that I use crop_img() on these thumbnails the 1th method created, it works. If I combine the two, or use them after one another, it doesn't. I've tried $this->image_lib->clear();, unsetting the config files. I even created two config files, just to be sure.</p> <p>I'm getting different all kind of errors from GD2, but the problem is, that after resize_img() creates the thumbnail, the crop_img() won't crop it. After that it all goes south, and the next images can't be opened. Write premissions are checked, both on folder and files.</p> <blockquote> <p>Unable to save the image. Please make sure the image and file directory are writable. The path to the image is not correct. Your server does not support the GD function required to process this type of image.</p> </blockquote> <p>Full code:</p> <pre><code>&lt;?PHP class Imagetest extends MY_Controller { function __construct() { parent::__construct(); $this-&gt;load-&gt;library('image_lib'); } function index() { $testimg1 = 'uploads/test/1.png'; $testimg2 = 'uploads/test/2.png'; $testimg3 = 'uploads/test/3.png'; $this-&gt;resize_img($testimg1); $this-&gt;crop_img($testimg1); $this-&gt;resize_img($testimg2); $this-&gt;crop_img($testimg2); $this-&gt;resize_img($testimg3); $this-&gt;crop_img($testimg3); } function image_thumb_name($img = null) { if(!empty($img)) { $exploded = explode('.', $img); return $exploded['0'] . '_thumb.' . $exploded['1']; } } function get_axis($img) { // Default values $output['x_axis'] = 0; $output['y_axis'] = 0; // Settings $config['height'] = 132; $config['width'] = 176; if ($img_dim = getimagesize($img)) { list($thumb_width, $thumb_height) = $img_dim; } else { echo '&lt;h1&gt; ERROR HERE TOO&lt;/h1&gt;'; return false; } if ($thumb_width &gt; $config['width']) { $output['x_axis'] = (($thumb_width - $config['width']) / 2) ; } else if ($thumb_height &gt; $config['height']) { $output['y_axis'] = (($thumb_height - $config['height']) / 2); } return $output; } function resize_img($img) { $config = array(); echo 'Processing: '. $img .'&lt;br/&gt;'; // Debug if ($img_dim = getimagesize($img)) { list($image_width, $image_height) = $img_dim; } else { echo '&lt;h1&gt; ERROR HERE &lt;/h1&gt;'; } // create a thumbnail $config['image_library'] = 'GD2'; $config['source_image'] = $img; $config['quality'] = 100; $config['height'] = 132; $config['width'] = 176; $config['create_thumb'] = TRUE; $config['maintain_ratio']= TRUE; $config['master_dim'] = ($image_width &gt; $image_height) ? 'height' : 'width'; $this-&gt;image_lib-&gt;initialize($config); if (!$this-&gt;image_lib-&gt;resize()) { echo $this-&gt;image_lib-&gt;display_errors(); } echo '&lt;img src="../'.$this-&gt;image_thumb_name($img).'" /&gt; &lt;br/&gt;'; // Debug $this-&gt;image_lib-&gt;clear(); unset($config); } function crop_img($img) { $config2 = array(); // Crop that thumbnail $config2['image_library'] = 'GD2'; $config2['quality'] = 100; $config2['height'] = 132; $config2['width'] = 176; $config2['source_image'] = $this-&gt;image_thumb_name($img); $axis = $this-&gt;get_axis($config2['source_image']); $config2['x_axis'] = $axis['x_axis']; $config2['y_axis'] = $axis['y_axis']; $config2['maintain_ratio'] = FALSE; $config2['create_thumb'] = FALSE; $this-&gt;image_lib-&gt;initialize($config2); if (!$this-&gt;image_lib-&gt;crop()) { echo $this-&gt;image_lib-&gt;display_errors(); } echo '&lt;img src="../'.$config2['source_image'].'" /&gt; &lt;br/&gt;'; // Debug $this-&gt;image_lib-&gt;clear(); unset($config2); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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