1

Most of my images are pretty much the same size but just a little difference pushes down and spoils the whole grid. I would like to make all images same size but do not wish to use anything like timthumb. Setting a width or height does not help at all since it's img-responsive and width and height changes on different devices with different screen sizes. Any help would be much appreciated.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • share your code so that we can help you better and will know what have you tried. – Bhushan Kawadkar Jul 18 '14 at 05:44
  • It will be helpful to see your code but you can use a responsive reset so that the grid doesn't break if there are differences in the heights of your images. See my answer here: http://stackoverflow.com/questions/24571062/gap-in-bootstap-stacked-rows/24571644#24571644 – jme11 Jul 18 '14 at 05:48

1 Answers1

0

The code below is a script I use in my CMS Made Simple applications. Put the script in the directory where your images are and call it like this:

http://www.example.com/images/imachecache.php/image1.jpg?w=160&h=120

This will resize the image proportionally, and crop (at the center of the image) if necessary. It will also create cache files for the thumbnails, and rebuild the cache file if the original image has been modified.

<?php

// Imagecache.php
$pi = $_SERVER['PATH_INFO'];
$w = $_GET['w'];
$h = $_GET['h'];
$bn = basename($pi);
$src = realpath(sprintf('%s/%s', __DIR__, $pi));
$cache = sprintf('%s/.imagecache/%s.%dx%d.jpg', __DIR__, $pi, $w, $h);
$cachedir = dirname($cache);
if (!is_dir($cachedir)) mkdir($cachedir, 0755, true);

// Create cache file
if (!file_exists($cache) || filemtime($cache) < filemtime($src)) {
    list($wo, $ho) = getimagesize($src);
    $im = imagecreatefromstring(file_get_contents($src));
    $ro = $wo / $ho;
    if ($w / $h > $ro) {
        $hn = $w / $ro;
        $wn = $w;
    } else {
        $wn = $h * $ro;
        $hn = $h;
    }
    $x = $wn / 2;
    $y = $hn / 2;
    $process = imagecreatetruecolor(round($wn), round($hn));
    imagecopyresampled($process, $im, 0, 0, 0, 0, $wn, $hn, $wo, $ho);
    $thumb = imagecreatetruecolor($w, $h);
    imagecopyresampled($thumb, $process, 0, 0, ($x - ($w / 2)), ($y - ($h / 2)), $w, $h, $w, $h);
    imagedestroy($process);
    imagedestroy($im);
    imagejpeg($thumb, $cache);
}

header('Content-Type: image/jpeg');
header('Content-Length: ' . filesize($cache));
readfile($cache);