Skip to content

Instantly share code, notes, and snippets.

@amarullz
Last active May 18, 2022
Embed
What would you like to do?
Proportional Image Resize in PHP

Proportional Image Resize in PHP

Penjelasan algoritma dapat dibaca di: https://amarullz.com/2022/05/14/algoritma-resize-gambar-secara-proporsional-contoh-pada-php/

Image Example, dapat didownload di: https://www.hippopx.com/en/city-landscape-night-urban-landscape-city-%E2%80%8B%E2%80%8Blights-306960 License to use Creative Commons Zero – CC0 Hippopx

License

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.

<?php
// Include class file
include_once "PropResize.php";
// Buat GDImage dari jpeg
$im = imagecreatefromjpeg("Hippopx.jpg");
// ScaleWidth:
$im2 = PropResize::resize($im, 320, 0, PropResize::ScaleWidth);
// ScaleHeight:
// $im2 = PropResize::resize($im, 0, 240, PropResize::ScaleHeight);
// Fit:
// $im2 = PropResize::resize($im, 320, 240, PropResize::Fit);
// Cover
// $im2 = PropResize::resize($im, 320, 240, PropResize::Cover);
// Set Header Content-type sebagai image/jpeg
header('content-type:image/jpeg');
// Output image hasil resize
imagejpeg($im2, null, 80);
<?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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment