Resize image by width/height

Uses GD to resize an image to a given size. Read more.

php
fork add to bundle source
function resize_image($file, $to, $w, $h, $crop = true) { list($width, $height) = getimagesize($file); $r = $width / $height; if ($crop) { if ($width > $height) { $width = ceil($width-($width*abs($r-$w/$h))); } else { $height = ceil($height-($height*abs($r-$w/$h))); } $newwidth = $w; $newheight = $h; } else { if ($w/$h > $r) { $newwidth = $h*$r; $newheight = $h; } else { $newheight = $w/$r; $newwidth = $w; } } $src = imagecreatefromjpeg($file); $dst = imagecreatetruecolor($newwidth, $newheight); imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); return imagejpeg($dst, $to); }

usage

load_function('resize_image'); resize_image('/tmp/image.jpg', '/tmp/resized.jpg', 100, 100); // Resized image will reside under /tmp/resized.jpg
← how to use this in app?
create log ...inout