Welcome Guest, Not a member yet? Register   Sign In
square thumbnails
#1

[eluser]samseko[/eluser]
Like others i have been trying to get square thumbnails from images of different width and height proportions and such.

My solution may be a little raw but it does the trick without the need to crop and simply resizes the image based on the max width or height limit, then fills the additonal area with whitespace to make an exact square:

Code:
$destination_pic = '/home/profile/public_html/images/cache/'.$image_name.'.jpg';

// get the width and height of original image
list($width, $height) = getimagesize($destination_pic);

// 100px x 100px dimensions for new square thumbnail via $dim
$dim = 100;
$box_w = $width;
$box_h = $height;

if ($width > $height) {
    $box_x = intval(($width - $height) / 2);
    $box_w = $box_h;
} elseif ($height >= $width) {
    $box_y = intval(($height - $width) / 2);
    $box_h = $box_w;
}
$image_new = imagecreatetruecolor($dim, $dim);
$white = imagecolorallocate($image_new, 255, 255, 255);
imagefill($image_new, 0, 0, $white);

imagecopyresampled($destination_pic, $destination_pic, 0, 0, $box_x, $box_y, $dim, $dim, $box_w, $box_h);

// resample square
$image_new = imagecreatetruecolor($dim, $dim);
// setting the background to white
$white = imagecolorallocate($image_new, 255, 255, 255);
imagefill($image_new, 0, 0, $white);

$image = imagecreatefromjpeg($destination_pic);
list($width, $height) = getimagesize($destination_pic);
imagecopy($image_new, $image, 0, 0, 0, 0, $width, $height);
//output
imagejpeg($image_new, $destination_pic, 100);

$this->load->library('image_lib');

$config['source_image'] = $destination_pic;
$config['wm_type'] = 'overlay';
$config['wm_overlay_path'] = '/home/profile/system/watermark.png';

$this->image_lib->initialize($config);

$this->image_lib->watermark();

readfile($destination_pic);
#2

[eluser]mattpointblank[/eluser]
Nice work. I ended up using phpThumb for this sort of thing.

A couple of points:

- Maybe add ability to accept different file formats (and thus switch to using imagecreatefromjpeg / imagecreatefrombmp etc)?

- I might be missing something, but where does $box_y get defined if the image is landscape?
#3

[eluser]samseko[/eluser]
[quote author="mattpointblank" date="1259693392"]

- Maybe add ability to accept different file formats (and thus switch to using imagecreatefromjpeg / imagecreatefrombmp etc)?
[/quote]

Good idea, though I'll leave that for someone else to contribute....

Quote:- I might be missing something, but where does $box_y get defined if the image is landscape?

I fixed that dogs breakfast up -- the image manipulation class does the initial resize now.

Code:
$destination_pic = '/home/profile/public_html/images/cache/'.$image_name.'.jpg';

// initialise the Image Manipulation class
$this->load->library('image_lib');

// width and height required
$dimx = 200;
$dimy = 200;

// resize image

$config['image_library'] = 'gd2';
$config['source_image'] = $destination_pic;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $dimx;
$config['height'] = $dimy;
$config['master_dim'] =    auto;

$this->image_lib->initialize($config);            
$this->image_lib->resize();

$this->image_lib->clear();

list($width, $height) = getimagesize($destination_pic);
// echo $width; echo $height;

// resample square
$image_new = imagecreatetruecolor($dimx, $dimy);
$white = imagecolorallocate($image_new, 255, 255, 255);
imagefill($image_new, 0, 0, $white);

$image = imagecreatefromjpeg($destination_pic);
list($width, $height) = getimagesize($destination_pic);
imagecopy($image_new, $image, 0, 0, 0, 0, $width, $height);
//output
imagejpeg($image_new, $destination_pic, 100);


$config['source_image'] = $destination_pic;
$config['wm_type'] = 'overlay';
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'center';
$config['wm_overlay_path'] = '/home/profile/system/profile-watermark.png';

$this->image_lib->initialize($config);

$this->image_lib->watermark();

readfile($destination_pic);




Theme © iAndrew 2016 - Forum software by © MyBB