Skip to content

Instantly share code, notes, and snippets.

@ademalp
Created June 7, 2019 17:06
Show Gist options
  • Save ademalp/0a0d3aa80c9a0ca63f16520cb7d170f2 to your computer and use it in GitHub Desktop.
Save ademalp/0a0d3aa80c9a0ca63f16520cb7d170f2 to your computer and use it in GitHub Desktop.
<?php
function image_thumb($file, $width = 460, $height = 0, $save = true, $quality = 80)
{
$qual = round(9 - ($quality / (100 / 9)));
//log_message('debug',"Kalite: $quality $qual");
$i = $file;
$x = $width;
$y = $height;
if (is_file(FCPATH . $i)) {
$thumb_marker = '_thumber';
$parts = pathinfo($file);
$tname = $parts['dirname'] . "/" . $parts['filename'] . $thumb_marker . "_" . $width . "." . $parts['extension'];
list($w, $h, $t) = getimagesize($i);
if (in_array($t, array(1, 2, 3))) {
if ($y == 0) {
$y = (int)($x * $h / $w);
}
if (($w / $h) >= ($x / $y)) {
$b = ceil($x * $h / $w);
$a = ceil($w * $b / $h);
} else {
$a = ceil($w * $y / $h);
$b = ceil($a * $h / $w);
}
$bx = ceil(($y - $b) / 2);
$ax = ceil(($x - $a) / 2);
if (IMAGE_THUMB_CONTROL && is_file(FCPATH . $tname)) {
return $tname;
} else {
$source = imagecreate($w, $h);
switch ($t) {
case 1:
$source = imagecreatefromgif($i);
break;
case 2:
$source = imagecreatefromjpeg($i);
break;
case 3:
$source = imagecreatefrompng($i);
break;
}
$thumb = imagecreatetruecolor($x, $y);
if ($t == 3) {
imagealphablending($thumb, false);
$alpha = imagecolorallocatealpha($thumb, 0, 0, 0, 127);
imagefill($thumb, 0, 0, $alpha);
imagesavealpha($thumb, true);
} else {
$white = imagecolorallocate($thumb, 255, 255, 255);
imagefill($thumb, 0, 0, $white);
}
imagecopyresampled($thumb, $source, $ax, $bx, 0, 0, $a, $b, $w, $h);
if ($save !== true) {
switch ($t) {
case 1:
header('Content-Type: image/gif');
imagegif($thumb, NULL);
break;
case 2:
header('Content-Type: image/jpeg');
imagejpeg($thumb, NULL, $quality);
break;
case 3:
header('Content-Type: image/png');
imagepng($thumb, NULL, $qual);
break;
}
imagedestroy($thumb);
return true;
} else {
switch ($t) {
case 1:
imagegif($thumb, FCPATH . $tname);
break;
case 2:
imagejpeg($thumb, FCPATH . $tname, $quality);
break;
case 3:
imagepng($thumb, FCPATH . $tname, $qual);
break;
}
imagedestroy($thumb);
return $tname;
}
}
} else {
return false;
}
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment