Created
May 25, 2021 13:14
-
-
Save ishafiul/1c5eee9473a5b7ed3219e41dc738459e to your computer and use it in GitHub Desktop.
Image manipulation cropping, resizeing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function crop($target, $newcopy, $w, $h, $ext,$horizontal,$vertical){ | |
$ext = strtolower($ext); | |
$size = getimagesize($target); | |
$x = $size[0]; | |
$y = $size[1]; | |
if($x>$w*2 || $y>$h*2){ | |
list($w_orig, $h_orig) = getimagesize($target); | |
$scale_ratio = $w_orig / $h_orig; | |
if (($w / $h) > $scale_ratio) { | |
$w = $h * $scale_ratio; | |
} else { | |
$h = $w / $scale_ratio; | |
} | |
$img = ""; | |
if ($ext == "gif"){ | |
$img = imagecreatefromgif($target); | |
} else if($ext =="png"){ | |
$img = imagecreatefrompng($target); | |
} else { | |
$img = imagecreatefromjpeg($target); | |
} | |
$tci = imagecreatetruecolor($w, $h); | |
//imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h) | |
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w*1.2, $h*1.2, $w_orig, $h_orig); | |
if ($ext == "gif"){ | |
header("Content-type: image/gif"); | |
imagegif($tci,'../../temp/resize.gif'); | |
} else if($ext =="png"){ | |
//header("Content-type: image/png"); | |
imagepng($tci,'../../temp/resize.png'); | |
} else { | |
//header("Content-type: image/jpeg"); | |
imagejpeg($tci,'../../temp/resize.jpg', 84); | |
} | |
$target = '../../temp/resize.'.$ext; | |
} | |
// Create an image from given image | |
$im = ""; | |
if ($ext == "gif"){ | |
$im = imagecreatefromgif($target); | |
//header("Content-type: image/gif"); | |
imagegif(cropAlign($im, $w, $h, $horizontal, $vertical),$newcopy); | |
} else if($ext =="png"){ | |
$im = imagecreatefrompng($target); | |
//header("Content-type: image/png"); | |
imagepng(cropAlign($im, $w, $h, $horizontal, $vertical),$newcopy); | |
} else { | |
$im = imagecreatefromjpeg($target); | |
//header("Content-type: image/jpeg"); | |
imagejpeg(cropAlign($im, $w, $h, $horizontal, $vertical),$newcopy); | |
} | |
unlink($target); | |
} | |
function cropAlign($image, $cropWidth, $cropHeight, $horizontalAlign = 'center', $verticalAlign = 'middle') { | |
$width = imagesx($image); | |
$height = imagesy($image); | |
$horizontalAlignPixels = calculatePixelsForAlign($width, $cropWidth, $horizontalAlign); | |
$verticalAlignPixels = calculatePixelsForAlign($height, $cropHeight, $verticalAlign); | |
return imageCrop($image, [ | |
'x' => $horizontalAlignPixels[0], | |
'y' => $verticalAlignPixels[0], | |
'width' => $horizontalAlignPixels[1], | |
'height' => $verticalAlignPixels[1] | |
]); | |
} | |
function calculatePixelsForAlign($imageSize, $cropSize, $align) { | |
switch ($align) { | |
case 'left': | |
case 'top': | |
return [0, min($cropSize, $imageSize)]; | |
case 'right': | |
case 'bottom': | |
return [max(0, $imageSize - $cropSize), min($cropSize, $imageSize)]; | |
case 'center': | |
case 'middle': | |
return [ | |
max(0, floor(($imageSize / 2) - ($cropSize / 2))), | |
min($cropSize, $imageSize), | |
]; | |
default: return [0, $imageSize]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment