imagecreatefromjpeg() เป็นฟังก์ชัน inbuilt ใน PHP ที่ใช้สร้างภาพใหม่จากไฟล์ JPEG ส่งคืนตัวระบุรูปภาพที่แสดงรูปภาพที่ได้รับจากชื่อไฟล์ที่กำหนด
ไวยากรณ์
resource imagecreatefromjpeg(string $filename)
พารามิเตอร์
imagecreatefromjpeg() ใช้พารามิเตอร์เพียงตัวเดียว $filename ที่เก็บชื่อรูปภาพหรือเส้นทางไปยังรูปภาพ JPEG
คืนค่า
imagecreatefromjpeg() ส่งคืนตัวระบุทรัพยากรรูปภาพเมื่อสำเร็จ และแสดงข้อผิดพลาดเป็นเท็จ
ตัวอย่างที่ 1
<?php
// Load an image from local drive/file
$img = imagecreatefromjpeg('C:\xampp\htdocs\test\1.jpeg');
// it will show the loaded image in the browser
header('Content-type: image/jpg');
imagejpeg($img);
imagedestroy($img);
?> ผลลัพธ์

ตัวอย่างที่ 2
<?php
// Load a JPEG image from local drive/file
$img = imagecreatefromjpeg('C:\xampp\htdocs\test\1(a).jpeg');
// Flip the image
imageflip($img, 1);
// Save the GIF image in the given path.
imagejpeg($img,'C:\xampp\htdocs\test\1(b).png');
imagedestroy($img);
?> ใส่รูปภาพ

ภาพที่ส่งออก

คำอธิบาย − ในตัวอย่างที่ 2 เราโหลดอิมเมจ jpeg จากพาธในเครื่องโดยใช้ imagecreatefromjpeg() การทำงาน. หลังจากนั้น เราใช้ฟังก์ชัน imageflip() เพื่อพลิกภาพ
ตัวอย่างที่ 3 – การจัดการข้อผิดพลาดระหว่างการโหลดภาพ JPEG
<?php
function LoadJpeg($imgname) {
/* Attempt to open */
$im = @imagecreatefromjpeg($imgname);
/* See if it failed */
if(!$im) {
/* Create a black image */
$im = imagecreatetruecolor(700, 300);
$bgc = imagecolorallocate($im, 0, 0, 255);
$tc = imagecolorallocate($im, 255,255, 255);
imagefilledrectangle($im, 0, 0, 700, 300, $bgc);
/* Output an error message */
imagestring($im, 20, 80, 80, 'Error loading ' . $imgname, $tc);
}
return $im;
}
header('Content-Type: image/jpeg');
$img = LoadJpeg('bogus.image');
imagejpeg($img);
imagedestroy($img);
?> ผลลัพธ์
