Tôi đang tìm kiếm một cách tốt hơn để xử lý tình huống này, vì vậy tôi đã sử dụng một vài chức năng khác nhau được tìm thấy trên internet.
Nhìn chung, khi nó hoạt động, chức năng nhanh nhất có xu hướng là hàm getjpegsize
mà James Relyea đã đăng trên trang PHP cho getimagesize
, đánh bại ranger
chức năng được cung cấp bởi Dejan ở trên.
//php.net/manual/en/feft.getimagesize.php#8879
Image #1 [787KB JPG on external older server]
getimagesize: 0.47042 to 0.47627 - 1700x2340 [SLOWEST]
getjpegsize: 0.11988 to 0.14854 - 1700x2340 [FASTEST]
ranger: 0.1917 to 0.22869 - 1700x2340
Image #2 [3MB PNG]
getimagesize: 0.01436 to 0.01451 - 1508x1780 [FASTEST]
getjpegsize: - failed
ranger: - failed
Image #3 [2.7MB JPG]
getimagesize: 0.00855 to 0.04806 - 3264x2448 [FASTEST]
getjpegsize: - failed
ranger: 0.06222 to 0.06297 - 3264x2448 * [SLOWEST]
Image #4 [1MB JPG]
getimagesize: 0.00245 to 0.00261 - 2031x1434
getjpegsize: 0.00135 to 0.00142 - 2031x1434 [FASTEST]
ranger: 0.0168 to 0.01702 - 2031x1434 [SLOWEST]
Image #5 [316KB JPG]
getimagesize: 0.00152 to 0.00162 - 1280x720
getjpegsize: 0.00092 to 0.00106 - 1280x720 [FASTEST]
ranger: 0.00651 to 0.00674 - 1280x720 [SLOWEST]
ranger
không thành công khi lấy 32768 byte trên Ảnh số 3, vì vậy tôi tăng nó lên 65536 và nó đã hoạt động để lấy kích thước thành công.
Tuy nhiên, có một số vấn đề vì cả ranger
và getjpegsize
đều bị giới hạn theo những cách khiến nó không đủ ổn định để sử dụng. Cả hai đều thất bại khi xử lý
một hình ảnh JPG lớn khoảng 3 MB, nhưng ranger
sẽ hoạt động sau khi thay đổi số lượng byte mà nó lấy. Ngoài ra, các lựa chọn thay thế này chỉ xử lý hình ảnh JPG, có nghĩa là cần phải sử dụng một điều kiện để chỉ sử dụng chúng trên JPG và getimagesize
trên các định dạng hình ảnh khác.
Ngoài ra, lưu ý rằng hình ảnh đầu tiên là trên một máy chủ cũ chạy phiên bản cũ PHP 5.3.2, trong đó 4 hình ảnh khác đến từ một máy chủ hiện đại [cPanel dựa trên đám mây với MultiPHP đã quay lại đến 5.4,45 để tương thích].
Điều đáng chú ý là máy chủ dựa trên đám mây đã làm tốt hơn rất nhiều với getimagesize
đã đánh bại ranger
, trên thực tế đối với cả 4 thử nghiệm trên máy chủ đám mây, ranger
là chậm nhất. Cả 4 người cũng đang kéo các hình ảnh từ cùng một máy chủ khi mã đang chạy, mặc dù các tài khoản khác nhau.
Điều này khiến tôi tự hỏi liệu lõi PHP đã cải thiện trong phiên bản 5.4 hay nếu phiên bản Apache có yếu tố. Ngoài ra, nó có thể giảm khả năng sẵn sàng từ máy chủ và máy chủ tải. Chúng ta đừng quên cách các mạng đang nhận được nhanh hơn và nhanh hơn mỗi năm, vì vậy có lẽ vấn đề tốc độ đang trở nên ít quan tâm hơn.
Vì vậy, kết quả cuối cùng và câu trả lời của tôi là để hỗ trợ đầy đủ cho tất cả các định dạng hình ảnh web và để vẫn đạt được kích thước hình ảnh siêu nhanh, tốt nhất nên hút nó lên và sử dụng getimagesize
và sau đó lưu trữ kích thước hình ảnh [nếu những hình ảnh này sẽ được kiểm tra nhiều lần] trong bảng cơ sở dữ liệu. Trong kịch bản đó, chỉ có lần kiểm tra
đầu tiên sẽ chịu chi phí lớn hơn, nhưng các yêu cầu tiếp theo sẽ tối thiểu và nhanh hơn bất kỳ chức năng nào đọc các tiêu đề hình ảnh.
Như với bất kỳ bộ nhớ đệm nào, nó chỉ hoạt động tốt nếu nội dung không thay đổi và có một cách để kiểm tra xem có phải là một thay đổi hay không. Vì vậy, một giải pháp khả thi là chỉ kiểm tra các tiêu đề của URL hình ảnh khi kiểm tra bộ đệm và nếu khác, hãy đổ phiên bản đã lưu vào bộ nhớ cache và lấy lại với getimagesize
.
[PHP 4, PHP 5, PHP 7, PHP 8]
getimagesize — Get the size of an image
Description
getimagesize[string $filename
, array &$image_info
= null
]: array|false
getimagesize[] can also return some more information in image_info
parameter.
Caution
This function expects filename
to be a valid image file. If a non-image file is supplied, it may be incorrectly detected as an image and the function will return successfully, but the array may contain nonsensical values.
Do not use getimagesize[] to check that a given file is a valid image. Use a purpose-built solution such as the Fileinfo extension instead.
Note: Note that JPC and JP2 are capable of having components with different bit depths. In this case, the value for "bits" is the highest bit depth encountered. Also, JP2 files may contain
multiple JPEG 2000 codestreams
. In this case, getimagesize[] returns the values for the first codestream it encounters in the root of the file.
Note: The information about icons are retrieved from the icon with the highest bitrate.
Note: GIF images consist of one or more frames, where each frame may only occupy part of the image. The size of the image which is reported by getimagesize[] is the overall size [read from the logical screen descriptor].
Parameters
filename
This parameter specifies the file you wish to retrieve information about. It can reference a local file or [configuration permitting] a remote file using one of the supported streams.
image_info
This optional parameter allows you to extract some extended information from the image file. Currently, this will return the different JPG APP markers as an associative array. Some programs use these APP markers to embed text information in images. A very common one is to embed » IPTC information in the APP13 marker. You can use the iptcparse[] function to parse the binary APP13 marker into something readable.
Note:
The
image_info
only supports JFIF files.
Return Values
Returns an array with up to 7 elements. Not all image types will include the channels
and bits
elements.
Index 0 and 1 contains respectively the width and the height of the image.
Note:
Some formats may contain no image or may contain multiple images. In these cases, getimagesize[] might not be able to properly determine the image size. getimagesize[] will return zero for width and height in these cases.
Index 2 is one of the IMAGETYPE_XXX constants indicating the type of the image.
Index 3 is a text string with the correct height="yyy" width="xxx"
string that can be
used directly in an IMG tag.
mime
is the correspondant MIME type of the image. This information can be used to deliver images with the correct HTTP Content-type
header:
Example #1 getimagesize[] and MIME types
kazuya ¶
8 years ago
i made function img_resize[$path,$tmp_name,$new_name,$new_width]
this could be useful.
cloned at clonedmadman dot com ¶
14 years ago
Well, I am making a script which will resize the image when uploaded, however, i am making a multi-uploader, so i came across with a problem: an efficient way of getting a pictures height and width and storing them in an array to resize later. This is what i came up with:
info at alex-lawrence dot com ¶
14 years ago
Could be useful [didn´t know where to post it]:
function getImageErrors[ $filename, $type = "", $minWidth = 0, $minHeight = 0, $maxWidth = 0, $maxHeight = 0, $maxFileSize = 0 ]
{
$errors = array[];
if [ file_exists[ $filename ] ]
{
$ending = substr[ $filename, strpos[ $filename, "." ] ];
if [ is_array[ $type ] ]
{
$isTypeOf = false;
foreach[ $type as $eachtype ]
{
if [ $ending == $eachtype ]
{
$isTypeOf = true;
}
}
if [ ! $isTypeOf ]
{
$errors[ 'type' ] = $ending;
}
}
elseif [ $type != "" ]
{
if [ $ending != $type ]
{
$errors[ 'type' ] = $ending;
}
}
$size = getimagesize[ $filename ];
if [ $size[ 0 ] < $minWidth ]
{
$errors[ 'minWidth' ] = $size[ 0 ];
}
if [ $size[ 1 ] < $minHeight ]
{
$errors[ 'minHeight' ] = $size[ 1 ];
}
if [ [ $maxWidth > $minWidth ] && [ $size[ 0 ] > $maxWidth ] ]
{
$errors[ 'maxWidth' ] = $size[ 0 ];
}
if [ [ $maxHeight > $minHeight ] && [ $size[ 1 ] > $maxHeight ] ]
{
$errors[ 'maxHeight' ] = $size[ 1 ];
}
if [ [ $maxFileSize > 0 ] && [ filesize[ $filename ] > $maxFileSize ] ]
{
$errors[ 'maxFileSize' ] = filesize[ $filename ];
}
}
else
{
$errors[ 'filename' ] = "not existing";
}
return [ count[ $errors ] > 0 ? $errors : null ];
}
utilmind ¶
11 years ago
Here is the function which determines whether the PNG image contains alpha or not:
The color type of PNG image is stored at byte offset 25. Possible values of that 25'th byte is:
* 0 - greyscale
* 2 - RGB
* 3 - RGB with palette
* 4 - greyscale + alpha
* 6 - RGB + alpha
Jesus Zamora ¶
11 years ago
Returns a array with 4 elements.
The 0 index is the width of the image in pixels.
The 1 index is the height of the image in pixels.
The 2 index is a flag for the image type:
1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF[orden de bytes intel], 8 = TIFF[orden de bytes motorola], 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM.
The 3 index contains ' height="yyy" width="xxx" '
ajreading at classixshop dot com ¶
17 years ago
A simple piece of code i wrote to proportionally resize an image to a max height and width then display it
freecorvette at gmail dot com ¶
4 years ago
For some images, using getimagesize[] without the second parameter will return the correct info, but when you add the second parameter it will return false. This is most likely a bug [and it has been reported as such], but meanwhile, if you encounter this problem, a workaround is to use exif_read_data[].
Coodiss at w3bbix dot net ¶
17 years ago
diablx at hotmail dot com ¶ 18 years ago
Heres a easy way to scale images to the
that they are in
*this is broken up so anyone can understand it :]
I'm sorry for they other scripts, but I made one mistake about the image resizing... here is a working script !
Chủ Đề