Skip to content

Instantly share code, notes, and snippets.

@noone0
Created August 18, 2016 12:36
Show Gist options
  • Save noone0/2d5fcd6d82ab2078adcc8596137138fa to your computer and use it in GitHub Desktop.
Save noone0/2d5fcd6d82ab2078adcc8596137138fa to your computer and use it in GitHub Desktop.
Resize Image Gist
function resize($image, $width, $height, $mod,$waterMarkImage =false)
{
if ( ! file_exists($image)) exit;
$imageModified = @filemtime($image);
$image_properties = getimagesize($image);
if ( ! $image_properties) {
return false;
}
//---------------------------------------------
$image_width = $image_properties[0];
$image_height = $image_properties[1];
$image_ratio = $image_width / $image_height;
$image_type = $image_properties["mime"];
//---------------------------------------------
if ( ! $width && ! $height) {
$width = $image_width;
$height = $image_height;
}
if ( ! $width && $height) {
$width = round($height * $image_ratio);
}
if ( ! $height && $width) {
$height = round($width / $image_ratio);
}
if ($image_type == "image/jpeg") {
$thumb = imagecreatefromjpeg($image);
} else if ($image_type == "image/png") {
$thumb = imagecreatefrompng($image);
} else if ($image_type == "image/gif") {
$thumb = imagecreatefromgif($image);
} else {
return false;
}
//---------------------------------------------
if ($mod == 1) {
if ($width <= $image_width && $height > $image_height) {
$img_new_width = $width;
$img_new_height = round($img_new_width / $image_ratio);
$canvas_width = $img_new_width;
$canvas_height = $img_new_height;
} else if ($height <= $image_height && $width > $image_width) {
$img_new_height = $height;
$img_new_width = round($img_new_height * $image_ratio);
$canvas_width = $img_new_height;
$canvas_height = $img_new_height;
} else {
$w_r = $image_width / $width;
$h_r = $image_height / $height;
if ($w_r <= $h_r) {
$img_new_height = $height;
$img_new_width = round($img_new_height * $image_ratio);
} else {
$img_new_width = $width;
$img_new_height = round($img_new_width / $image_ratio);
}
$canvas_width = $img_new_width;
$canvas_height = $img_new_height;
}
} else if ($mod == 2) {
if ($width <= $image_width && $height > $image_height) {
$img_new_height = $height;
$img_new_width = round($img_new_height * $image_ratio);
$canvas_width = $width;
$canvas_height = $img_new_height;
} else if ($height <= $image_height && $width > $image_width) {
$img_new_width = $width;
$img_new_height = round($img_new_width / $image_ratio);
$canvas_width = $img_new_width;
$canvas_height = $height;
} else {
$w_r = $image_width / $width;
$h_r = $image_height / $height;
if ($w_r <= $h_r) {
$img_new_width = $width;
$img_new_height = round($img_new_width / $image_ratio);
$canvas_width = $img_new_width;
$canvas_height = $height;
} else {
$img_new_height = $height;
$img_new_width = round($img_new_height * $image_ratio);
$canvas_width = $width;
$canvas_height = $img_new_height;
}
}
} else if ($mod == 3) {
$img_new_width = $width;
$img_new_height = $height;
$canvas_width = $width;
$canvas_height = $height;
} else if ($mod == 4) {
if ($width <= $image_width || $height <= $image_height) {
$w_r = $image_width / $width;
$h_r = $image_height / $height;
if ($w_r >= 1 && $h_r >= 1) {
if ($w_r > $h_r) {
$img_new_width = $width;
$img_new_height = round($img_new_width / $image_ratio);
} else {
$img_new_height = $height;
$img_new_width = round($img_new_height * $image_ratio);
}
} else if ($w_r >= 1) {
$img_new_width = $width;
$img_new_height = round($img_new_width / $image_ratio);
} else if ($h_r >= 1) {
$img_new_height = $height;
$img_new_width = round($img_new_height * $image_ratio);
}
$canvas_width = $img_new_width;
$canvas_height = $img_new_height;
} else {
$img_new_width = $image_width;
$img_new_height = $image_height;
$canvas_width = $image_width;
$canvas_height = $image_height;
}
} else {
return false;
}
$canvas = imagecreatetruecolor($canvas_width, $canvas_height);
if ($img_new_width < $canvas_width) {
$pos_x1 = 0;
$pos_x2 = round(($img_new_width - $canvas_width) / 2);
} else {
$pos_x1 = round(($canvas_width - $img_new_width) / 2);
$pos_x2 = 0;
}
if ($img_new_height < $canvas_height) {
$pos_y1 = 0;
$pos_y2 = round(($img_new_height - $canvas_height) / 2);
} else {
$pos_y1 = round(($canvas_height - $img_new_height) / 2);
$pos_y2 = 0;
}
imagecopyresampled($canvas, $thumb, $pos_x1, $pos_y1, $pos_x2, $pos_y2, $img_new_width, $img_new_height, $image_width, $image_height);
if($waterMarkImage)
{
$overlay_gd_image = imagecreatefrompng( $waterMarkImage );
$overlay_width = imagesx( $overlay_gd_image ) ;
$overlay_height = imagesy( $overlay_gd_image ) ;
$diffAspect = round(($img_new_width) / $overlay_width);
$img_paste_x = 0;
while($img_paste_x < $img_new_width){
$img_paste_y = 0;
while($img_paste_y < $img_new_height){
imagecopy($canvas, $overlay_gd_image, $img_paste_x, $img_paste_y, 0, 0, $overlay_width, $overlay_height);
$img_paste_y += $overlay_height;
}
$img_paste_x += $overlay_width;
}
}
ob_clean();
header("Content-type: image/jpeg");
header("Pragma: public");
header("Cache-Control: public");
header("Last-Modified: ".gmdate("D, d M Y H:i:s",$imageModified)." GMT");
header('Content-type: image/jpeg');
imagejpeg($canvas, NULL, 90); // kalite %80
imagedestroy($canvas);
imagedestroy($thumb);
exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment