Here is the script that loads the image, resizes and crops it:
Code: Select all
<?php
include("config.php");
$fileName = $_GET['file'];
$maxSize = $_GET['size'];
$URL = $basePath . "/images/players/" . $fileName;
$info = getimagesize($URL);
$mime = image_type_to_mime_type($info[2]);
header("Content-type: $mime");
// create an image resource
$t_w = 100;
$t_h = 100;
switch($info[2])
{
case IMAGETYPE_GIF:
$src_image = imagecreatefromgif($URL);
break;
case IMAGETYPE_JPEG:
$src_image = imagecreatefromjpeg($URL);
break;
case IMAGETYPE_PNG:
$src_image = imagecreatefrompng($URL);
break;
}
/*
$src_image = imagecreatefromjpeg($URL);
if(!$src_image){
$src_image = imagecreatefromjpeg("images/not_available.jpg");
}*/
// store the width and height values of the image
$orig_h = imagesy($src_image);
$orig_w = imagesx($src_image);
// test dimensions of image resource and set thumbnail width height to maintain correct aspect ratio
if($orig_w > $orig_h){
$thumb_w = $maxSize;
$thumb_h = $orig_h * ($maxSize / $orig_w);
}
if($orig_w < $orig_h){
$thumb_h = $maxSize;
$thumb_w = $orig_w * ($maxSize / $orig_h);
}
if($orig_w == $orig_h){
$thumb_w = $maxSize;
$thumb_h = $maxSize;
}
$x_mid = $thumb_w/2;
$y_mid = $thumb_h/2;
$dst_image = imagecreatetruecolor($thumb_w,$thumb_h);
imagecopyresampled($dst_image,$src_image,0,0,($x_mid-($t_w/2)),($y_mid-($t_h/2)),$thumb_w,$thumb_h,$orig_w,$orig_h);
switch($info[2])
{
case IMAGETYPE_GIF:
imagegif($dst_image);
break;
case IMAGETYPE_JPEG:
imagejpeg($dst_image);
break;
case IMAGETYPE_PNG:
imagepng($dst_image);
break;
}
imagedestroy($dst_image);
imagedestroy($src_img);
?>
Code: Select all
<img src="image_crop.php?file=filename.jpg&size=150" />
Can someone please help?