dongpi9494 2011-08-31 19:42
浏览 66
已采纳

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 ;
  • 写回答

3条回答 默认 最新

  • drnycqxwz63508434 2011-08-31 19:56
    关注
    $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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法