Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- resize_image('myImg.png', 150, 150);
- function resize_image($src_name, $w, $h)
- {
- $src = imagecreatefrompng($src_name);
- // Getting som imageinfo
- list($width, $height, $image_type) = getimagesize($src_name);
- // keeping the ratio
- if($height > $width) {
- $ratio = $h / $height;
- $new_width = $width * $ratio;
- $new_height = $height * $ratio;
- }
- else {
- $ratio = $w / $width;
- $new_width = $width * $ratio;
- $new_height = $height * $ratio;
- }
- // Creates a temp-image
- $temp = imagecreatetruecolor($new_width, $new_height);
- // Stängar av alphablendnig för att kunna spara alphakanalen
- imagealphablending($temp, false);
- // Fixar filnamn
- $newName = explode('.', $src_name);
- $newName = 'images/'.$newName[0];
- $newName .= '.png'; // Saves png
- // resizing
- imagecopyresampled($temp, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
- // Activates the alpha-channel
- imagesavealpha($temp, true);
- // Saves the image
- imagepng($temp, $newName);
- // destroy the objects
- imagedestroy($src);
- imagedestroy($temp);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement