Last active
November 4, 2019 14:17
-
-
Save rachhen/98a5d5e8435d8c7dc01b9df529ee3bcc to your computer and use it in GitHub Desktop.
Get image from url, resize and return back with resized
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| if (!isset($_GET['size'])) { | |
| echo "Please provide size image. Ex: get_image_by_size.php?size=250x150"; | |
| exit; | |
| } | |
| $size = $_GET['size']; | |
| list($w, $h) = explode('x', $size); | |
| $url = "https://cdn.pixabay.com/photo/2019/10/29/15/57/vancouver-4587302_960_720.jpg"; | |
| $im = imagecreatefromstring(file_get_contents($url)); | |
| $url_info = pathinfo($url); | |
| if ($im !== false) { | |
| header("Content-Type: image/{$url_info["extension"]}"); | |
| $width = imagesx($im); | |
| $height = imagesy($im); | |
| $r = $width / $height; | |
| if ($w / $h > $r) { | |
| $newwidth = $h * $r; | |
| $newheight = $h; | |
| } else { | |
| $newheight = $w / $r; | |
| $newwidth = $w; | |
| } | |
| $img = imagecreatetruecolor($newwidth, $newheight); | |
| imagealphablending($img, false); | |
| imagesavealpha($img, true); | |
| $transparent = imagecolorallocatealpha($img, 255, 255, 255, 127); | |
| imagefilledrectangle($img, 0, 0, $newwidth, $newheight, $transparent); | |
| imagecopyresized($img, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); | |
| imagejpeg($img); | |
| imagedestroy($img); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment