Gambar Berita Swarakalibata V.3.3 agar otomatis beresolusi 600x400 pxDibuat
Login Terakhir
11 Bulan lalu,
Telah Dilihat 1730 Kali
| |
Sultan Account |
|
|
Raihan Al Karim
Mengatakan :
Sultan Account
Bagaimana cara agar foto berita yang dimasukkan otomatis beresolusi 600x400 px min ? Mohon bantuannya |
|
| PHPMU.COM Support commented on 22 Mar 2017 20:32:45 Sultan Account |
|
|
|
Assalam, ini saya kasih contoh kode untuk crop image jadi 600x400px nya mas,..
silahkan digabungkan ke swarakalibatanya,.. :)
<html>
<head>
<title>Upload Image Dan Potong</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
Pilih Gambar: <input type="file" name="image" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html><hr>
<?php
if (isset($_POST['submit'])){
$image = $_FILES;
$NewImageName = rand(4,10000)."-". $image['image']['name'];
$destination = realpath('images').'/';
move_uploaded_file($image['image']['tmp_name'], $destination.$NewImageName);
$image = imagecreatefromjpeg($destination.$NewImageName);
$filename = $destination.$NewImageName;
$thumb_width = 600;
$thumb_height = 400;
$width = imagesx($image);
$height = imagesy($image);
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect){
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}else{
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, $filename, 80);
echo "Pemotongan Selesai!"; die;
}
?>
#Source : http://stackoverflow.com/questions/1855996/crop-image-in-php |
|