PHP:使用getimagesize在数据库中输入图像尺寸

I have a number of images who's information is stored in a MySQL database. The images themselves are stored in a folder, but the database contains their URL as well as other pertinent data.

I came across the PHP getimagesize which I was thinking of using to populate the width= and height= properties of the img tags of each image. However, I have been told that the overhead for this (as there are hundreds of images) will slow down the page load substantially.

I'm curious if there is a way to use getimagesize to search out all the images from their information in the database, calculate their width and height, and then insert the data into their respective width and height fields in the database, Thus allowing a simple sql query to do the work?

Here is my database structure:

CREATE TABLE `secondary_images` (
  `imgId` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `primaryId` int(10) unsigned DEFAULT NULL,
  `imgURL` varchar(255) DEFAULT NULL,
  `width` varchar(255) DEFAULT NULL,
  `height` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`imgId`),
  KEY `primaryId` (`primaryId`),
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;
$image_folder = "images/";
$res = mysql_query('SELECT * FROM secondary_images')
while ($row = mysql_fetch_assoc($res)) {
    $size = getimagesize($image_folder.$row['imgUrl']);
    mysql_query("UPDATE secondary_images SET width='{$size[0]}', height='{$size[1]} WHERE imgId='{$row['imgId']}'");
}

Why do you want to put image size in html anyway?

There is no way to get size of image(s) if you don't want to use getimagesize(). I think it is fast enough when they're saved locally

This algorithm may help:

  1. Write a query that returns the URLs and imgId of images with a width of null
  2. For each of those images, use getimagesize() to get their image size
  3. Write an update query to update height and width by imageid

That said, I think it would be best to skip the idea of updating when a user hits the page. At some point, the initial insertion of image data is done -- can you use getimagesize() at that time, or perhaps in a separate program which performs the scan independently of your web site?