Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cdmz/049a3dc2e18b9959d912 to your computer and use it in GitHub Desktop.
Save cdmz/049a3dc2e18b9959d912 to your computer and use it in GitHub Desktop.
Class that generates images in custom size keeping the aspect ratio. If the image does not exist, will be generated and saved in the folder mentioned and printed on the screen. If the image already exists in the reported dimensions, the image will be printed on the screen.
<?php
class image{
public function createthumb_estadio($id, $name, $new_w, $new_h) {
$border=false; $transparency=true; $base64=false;
$name_ = $name;
$name = "images/estadios/{$id}/{$name}";
$newname = "images/estadios/$id/".$new_w."_".$new_h.$name_;
if(file_exists($newname)){
switch (exif_imagetype($newname)) {
case IMAGETYPE_PNG:
header('Content-Type: image/png');
echo file_get_contents($newname);
break;
case IMAGETYPE_GIF:
header('Content-Type: image/gif');
echo file_get_contents($newname);
break;
case IMAGETYPE_JPEG:
default:
header('Content-Type: image/jpeg');
echo file_get_contents($newname);
break;
}
exit;
}
if(!file_exists($name))
return false;
$arr = explode(".",$name);
$ext = $arr[count($arr)-1];
if($ext=="jpeg" || $ext=="jpg"){
$img = @imagecreatefromjpeg($name);
} elseif($ext=="png"){
$img = @imagecreatefrompng($name);
} elseif($ext=="gif") {
$img = @imagecreatefromgif($name);
}
if(!$img)
return false;
$old_x = imageSX($img);
$old_y = imageSY($img);
if($old_x < 150 AND $old_y < 200){
$thumb_w = $old_x;
$thumb_h = $old_y;
}else if($old_x < 200 AND $old_y < 150){
$thumb_w = $old_x;
$thumb_h = $old_y;
}else{
$ops = $this->scaleImage($old_x,$old_y,$new_w,$new_h);
$thumb_w = $ops[0];
$thumb_h = $ops[1];
}
$new_img = ImageCreateTrueColor($thumb_w, $thumb_h);
if($transparency) {
if($ext=="png") {
imagealphablending($new_img, false);
$colorTransparent = imagecolorallocatealpha($new_img, 0, 0, 0, 127);
imagefill($new_img, 0, 0, $colorTransparent);
imagesavealpha($new_img, true);
} elseif($ext=="gif") {
$trnprt_indx = imagecolortransparent($img);
if ($trnprt_indx >= 0) {
//its transparent
$trnprt_color = imagecolorsforindex($img, $trnprt_indx);
$trnprt_indx = imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
imagefill($new_img, 0, 0, $trnprt_indx);
imagecolortransparent($new_img, $trnprt_indx);
}
}
} else {
Imagefill($new_img, 0, 0, imagecolorallocate($new_img, 255, 255, 255));
}
imagecopyresampled($new_img, $img, 0,0,0,0, $thumb_w, $thumb_h, $old_x, $old_y);
if($border) {
$black = imagecolorallocate($new_img, 0, 0, 0);
imagerectangle($new_img,0,0, $thumb_w, $thumb_h, $black);
}
if($base64) {
ob_start();
imagepng($new_img);
$img = ob_get_contents();
ob_end_clean();
$return = base64_encode($img);
} else {
if($ext=="jpeg" || $ext=="jpg"){
header("Content-type: image/jpeg");
imagejpeg($new_img);
imagejpeg($new_img,$newname);
} elseif($ext=="png"){
header("Content-type: image/png");
imagepng($new_img);
imagepng($new_img,$newname);
} elseif($ext=="gif") {
header("Content-type: image/gif");
imagegif($new_img);
imagegif($new_img,$newname);
}
}
}
public function scaleImage($x,$y,$cx,$cy) {
//Set the default NEW values to be the old, in case it doesn't even need scaling
list($nx,$ny)=array($x,$y);
//If image is generally smaller, don't even bother
if ($x>=$cx || $y>=$cx) {
//Work out ratios
if ($x>0) $rx=$cx/$x;
if ($y>0) $ry=$cy/$y;
//Use the lowest ratio, to ensure we don't go over the wanted image size
if ($rx>$ry) {
$r=$ry;
} else {
$r=$rx;
}
//Calculate the new size based on the chosen ratio
$nx=intval($x*$r);
$ny=intval($y*$r);
}
//Return the results
return array($nx,$ny);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment