Welcome Guest, Not a member yet? Register   Sign In
The problem with image_lib
#1

[eluser]codeDeveloper[/eluser]
Hi,

I use image_lib to create preview image.

Code:
$config['image_library'] = 'gd2';
$config['source_image'] = $fileName;
$config['maintain_ratio'] = TRUE;
$config['new_image'] = 'path';
$config['width'] = 640;
$config['height'] = 480;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();

If the image is less than 640 * 480, it stretches to 640 * 480, how fix this problem?


Unfortunately, the solutions are not found in the documentation.
#2

[eluser]dmorin[/eluser]
If you have $config['maintain_ratio'] = TRUE; then the image library should never stretch the original image to fit a new aspect ratio. Can you also post the actual full code you're using so we can attempt to recreate the problem.
#3

[eluser]codeDeveloper[/eluser]
Spread full code

Controller
Code:
<?php

class resize extends Controller {

    function __construct() {
        parent::Controller();    
    }
    
    function index() {
        $this->load->view ('resize/index');
    }
    
    function create () {
        $this->load->model ('resizemodel', 'model');
        $this->model->createAvatar('/dev.icq-mobi.ru/www/i/download.jpg');
        $this->load->view('resize/create');
    }
}
?>

Model
Code:
<?php
class resizemodel extends Model {
    
    public function __construct() {
        parent::Model();
    }
    
    public function createAvatar ($fileName) {
        $config['image_library'] = 'gd2';
        $config['source_image'] = $fileName;
        $config['maintain_ratio'] = TRUE;
        $config['new_image'] = '/dev.icq-mobi.ru/www/upload/big.jpg';
        $config['width'] = 640;
        $config['height'] = 480;
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
        $this->image_lib->clear();
        
        $config['image_library'] = 'gd2';
        $config['source_image'] = $fileName;
        $config['maintain_ratio'] = TRUE;
        $config['new_image'] = '/dev.icq-mobi.ru/www/upload/small.jpg';
        $config['width'] = 120;
        $config['height'] = 80;
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
    }
}
?>

Views think you do not need to spread
Example: http://dev.icq-mobi.ru/resize/
#4

[eluser]dmorin[/eluser]
Thanks. What are the dimensions of the original image you're using and what version of CI?
#5

[eluser]codeDeveloper[/eluser]
Please see the example that I gave http://dev.icq-mobi.ru/resize/ there is an image 16 * 16 and you can see that it stretches.
Using CodeIgniter 1.7
#6

[eluser]srisa[/eluser]
From what I understand of Image_lib library, images do stretch. And link provided by you is a working example of that. If you have access to the dimensions of the image beforehand , ensure that they are not less than 640/480. Or you can tinker with Image_lib library, orig_width and orig_height variables store the width and height of the original image.
#7

[eluser]dmorin[/eluser]
Not sure I understand. It get's larger, but it's not stretching it to 640 * 480 as you originally said. If you're resizing it, of course it's going to be larger!

Are you just wondering how to prevent the image from being enlarged?
#8

[eluser]dmorin[/eluser]
try

Code:
<?php
class resizemodel extends Model {
    
    public function __construct() {
        parent::Model();
    }
    
    public function createAvatar ($fileName) {
        $this->load->library('image_lib');

        $big_img = '/dev.icq-mobi.ru/www/upload/big.jpg';
        $small_img = '/dev.icq-mobi.ru/www/upload/small.jpg';
        list($width, $height, $type, $attr) = getimagesize($fileName);
            
        if ($width > 640 OR $height > 480)
        {
            $config['image_library'] = 'gd2';
            $config['source_image'] = $fileName;
            $config['maintain_ratio'] = TRUE;
            $config['new_image'] = $big_img;
            $config['width'] = 640;
            $config['height'] = 480;
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();
        }
        else
        {
            copy($fileName, $big_img);
        }
        
        if ($width > 120 OR $height > 80)
        {
            $config['image_library'] = 'gd2';
            $config['source_image'] = $fileName;
            $config['maintain_ratio'] = TRUE;
            $config['new_image'] = $small_img;
            $config['width'] = 120;
            $config['height'] = 80;
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
        }
        else
        {
            copy($fileName, $small_img);
        }
    }
}
?>

Edit: Moved the load image library out of the first if statement...oops. Also, I haven't actually tested this...there may be other errors. This is just a guide.
#9

[eluser]codeDeveloper[/eluser]
Thanks. I think there is any option which is responsible for such behavior, as well as changing the size of the big party is usually not used.
#10

[eluser]BaRzO[/eluser]
bump




Theme © iAndrew 2016 - Forum software by © MyBB