Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $file_gbr='1.jpg'; //contoh memakai jpg silakan update dengan keperluan
- $file_hasil='1_res.jpg';
- $width=300;
- $height=500;
- $center=TRUE;
- ResizeImage($file_gbr, $file_hasil, $width, $height, $center);
- echo "<img src='".$file_hasil."' />";
- function resizeImage($img0, $img1, $width1, $height1, $center=FALSE){
- list($width0, $height0) = getimagesize($img0);
- //sumber https://stackoverflow.com/questions/14649645/resize-image-in-php
- //$r = $width / $height;
- //perbandingan antara panjang lebar gambar sumber
- $resize0 = $width0 / $height0;
- //perbandingan antara panjang lebar gambar tujuan
- $resize1 = $width1 / $height1;
- logTxt("r: $resize0 | $resize1 ");
- if( $resize1 > $resize0){
- logTxt('-r1-');
- $newWidth = $height1 * $resize0 * $resize1;
- $newHeight= $height1;
- }else{
- logTxt('-r0-');
- $newHeight = $width1 / $resize0 * $resize1;
- $newWidth= $width1;
- }
- logTxt("h,w: ($newHeight|$newWidth)");
- //sesuaikan dengan tipe gbr
- $imgSrc = imagecreatefromjpeg($img0);
- if($center){
- //posisi x,y
- $x1= ceil( ($width1 - $newWidth) /2);
- $y1= ceil( ($height1 - $newHeight) /2);
- logTxt("w,h : $width1,$height1 | $newWidth,$newHeight");
- logTxt("x,y : $x1,$y1");
- //ukuran disesuaikan dgn request
- $imgTarget=imagecreatetruecolor($width1, $height1);
- imagecopyresized($imgTarget, $imgSrc, $x1, $y1, 0, 0, $newWidth, $newHeight, $width0, $height0);
- }else{
- //ukuran sesuai dengan ukuran baru
- $imgTarget=imagecreatetruecolor($newWidth, $newHeight);
- logTxt("not center : $newWidth, $newHeight, $width0, $height0");
- imagecopyresized($imgTarget, $imgSrc, 0, 0, 0, 0, ceil($newWidth), ceil($newHeight), $width0, $height0);
- }
- //save gambar
- imagejpeg($imgTarget, $img1);
- }
- function logTxt($str){
- $file='1.log';
- error_log($str."\n",3,$file);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement