|
<?php |
|
/* |
|
* Copyright 2022 Ahmad Amarullah (https://amarullz.com) |
|
* |
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
* you may not use this file except in compliance with the License. |
|
* You may obtain a copy of the License at |
|
* |
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
* |
|
* Unless required by applicable law or agreed to in writing, software |
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
* See the License for the specific language governing permissions and |
|
* limitations under the License. |
|
* |
|
*/ |
|
|
|
/** |
|
* Class untuk melakukan resize image secara proporsional |
|
*/ |
|
class PropResize |
|
{ |
|
/* Scale Type */ |
|
public const ScaleWidth = 1; |
|
public const ScaleHeight = 2; |
|
public const Fit = 3; |
|
public const Cover = 4; |
|
|
|
/** |
|
* Resize image secara proportional dari image resource |
|
* |
|
* @param GDImage $ImageSumber Image resource sumber |
|
* @param int $TargetWidth Target width (diacuhkan bila tipe=ScaleHeight) |
|
* @param int $TargetHeight Target height (diacuhkan bila tipe=ScaleWidth) |
|
* @param int $Type tipe resize yang akan dilakukan |
|
* @return GDImage Image resource hasil resize |
|
*/ |
|
public static function resize($ImageSumber, $TargetWidth, $TargetHeight, $Type) |
|
{ |
|
// Ambil ukuran sumber |
|
$WidthSumber = imagesx($ImageSumber); |
|
$HeightSumber = imagesy($ImageSumber); |
|
|
|
// Hitung rasio |
|
$RasioWidth = $TargetWidth / $WidthSumber; |
|
$RasioHeight = $TargetHeight / $HeightSumber; |
|
|
|
// Tentukan rasio yang digunakan |
|
$Rasio = $RasioWidth; |
|
if (($Type == self::ScaleHeight) || |
|
(($Type == self::Fit) && ($RasioHeight < $RasioWidth)) || |
|
(($Type == self::Cover) && ($RasioHeight > $RasioWidth)) |
|
) |
|
$Rasio = $RasioHeight; |
|
|
|
// Hitung ukuran resize |
|
$ResizeWidth = $Rasio * $WidthSumber; |
|
$ResizeHeight = $Rasio * $HeightSumber; |
|
|
|
// Siapkan koordinat target |
|
$TargetX = 0; |
|
$TargetY = 0; |
|
|
|
if ($Type == self::Fit || $Type == self::Cover) { |
|
// Hitung koordinat target untuk fit dan cover |
|
$TargetX = ($TargetWidth - $ResizeWidth) / 2; |
|
$TargetY = ($TargetHeight - $ResizeHeight) / 2; |
|
} else { |
|
// Bila bukan fit dan cover, set ukuran target |
|
// sesuai dengan ukuran resize |
|
$TargetWidth = $ResizeWidth; |
|
$TargetHeight = $ResizeHeight; |
|
} |
|
|
|
// Buat Image Target |
|
$ImageTarget = imagecreatetruecolor($TargetWidth, $TargetHeight); |
|
|
|
if ($Type == self::Fit) { |
|
// Pastikan fill target terlebih dahulu untuk fit |
|
$Hitam = imagecolorallocate($ImageTarget, 0, 0, 0); |
|
imagefill($ImageTarget, 0, 0, $Hitam); |
|
} |
|
|
|
// Gambar dan resize image sumber ke image target |
|
// Gunakan imagecopyresampled agar menggunakan smooth scaling |
|
imagecopyresampled( |
|
$ImageTarget, |
|
$ImageSumber, |
|
$TargetX, |
|
$TargetY, |
|
0, |
|
0, |
|
$ResizeWidth, |
|
$ResizeHeight, |
|
$WidthSumber, |
|
$HeightSumber |
|
); |
|
|
|
// Return Image Target |
|
return $ImageTarget; |
|
} |
|
} |