कोड छवि का आकार बदलने के लिए PHP का उपयोग करता है (वर्तमान में केवल jpeg.webp)। इस विधि का उपयोग करते हुए, आकार बदलने वाली छवि ब्राउज़र-साइड आकार बदलने की तुलना में बेहतर गुणवत्ता की होती है। नई डाउनसाइड इमेज का फाइल साइज भी छोटा है (डाउनलोड करने में तेज)।
कोड दो भागों में आता है:
- imageResizer () का उपयोग छवि को संसाधित करने के लिए किया जाता है
- loadimage () एक सरल प्रारूप में छवि url सम्मिलित करता है
$ratio_orig) ( $width = $height*$ratio_orig; ) else ( $height = $width/$ratio_orig; ) // This resamples the image $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg.webp($url); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); // Output the image imagejpeg.webp($image_p, null, 100); ) //works with both POST and GET $method = $_SERVER('REQUEST_METHOD'); if ($method == 'GET') ( imageResize($_GET('url'), $_GET('w'), $_GET('h')); ) elseif ($method == 'POST') ( imageResize($_POST('url'), $_POST('w'), $_POST('h')); ) // makes the process simpler function loadImage($url, $width, $height)( echo 'image.php?url=', urlencode($url) , '&w=',$width, '&h=',$height; ) ?>
प्रयोग
उपरोक्त कोड image.php नामक फाइल में होगा।
चित्र इस तरह प्रदर्शित होंगे:
