Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP – ดึง/ตั้งค่า Resolution ของรูปภาพด้วย imageresolution()

ฟังก์ชัน imageresolution() ใช้ดึงหรือตั้งค่า resolution (DPI - dots per inch) ของรูปภาพ หากไม่ระบุพารามิเตอร์เพิ่มเติม จะคืนค่า resolution ปัจจุบันเป็น indexed array หากระบุพารามิเตอร์เดียว จะตั้งค่า width และ height ให้เท่ากับค่านั้น

Resolution ใช้เป็น meta information เท่านั้นเมื่ออ่าน/เขียนไฟล์ที่รองรับ (PNG, JPEG) ไม่ส่งผลต่อการวาดภาพ ค่าเริ่มต้น 96 DPI

รูปแบบการใช้งาน (Syntax)

array|bool imageresolution(GdImage $image, int $res_x = null, int $res_y = null)

พารามิเตอร์

  • $image – GdImage resource
  • $res_x – horizontal resolution (DPI)
  • $res_y – vertical resolution (DPI)

ค่าที่คืนกลับ

คืน indexed array ของ resolution

ตัวอย่าง 1: ตั้งค่าและดึงค่า

<?php
$img = imagecreatetruecolor(100, 100);
imageresolution($img, 200);
print_r(imageresolution($img));
imageresolution($img, 300, 72);
print_r(imageresolution($img));
?>

ผลลัพธ์

Array ( [0] => 200 [1] => 200 )
Array ( [0] => 300 [1] => 72 )

ตัวอย่าง 2: โหลดรูปและตั้งค่า resolution

<?php
$img = imagecreatefrompng('C:\xampp\htdocs\Images\img34.png');
imageresolution($img, 300, 100);
$imageresolution = imageresolution($img);
print("
" . print_r($imageresolution, true) . "
"); ?>

ผลลัพธ์

Array ( [0] => 300 [1] => 100 )