【问题标题】:How to determine the picture size, then selectively insert into the database? php如何确定图片大小,然后选择性地插入数据库? php
【发布时间】:2010-11-15 09:46:27
【问题描述】:

我有 5 张图片,img1.jpg、img2.png、img3.gif、img4.jpg、img5.png。

如何确定图片大小,如果是横向图片(宽度大于高度)宽度>=300px 高度>=200,则插入数据库?

否则图片是Vertical图片或者宽度小于300px高度小于200px,不插入数据库吗?

<?php
$links = array("img1.jpg", "img2.png", "img3.gif", "img4.jpg", "img5.png");
$sizearray = array();
$count = count($links);
for($i = 0; $i < $count; $i++) {
    $size = getimagesize($links[$i]);
    list($width, $height) = $size;
    $sizearray[$links[$i]] = array("width" => $width, "height" => $height);
}
print_r($sizearray);
// which will print out: Array ( [img1.jpg] => Array ( [width] => 300 [height] => 400 ) [img2.png] => Array ( [width] => 680 [height] => 330 ) [img3.gif] => Array ( [width] => 50 [height] => 50 )[img4.jpg] => Array ( [width] => 400 [height] => 250 )[img5.png] => Array ( [width] => 400 [height] => 300 ))
?>


<?php
mysql_query("INSERT INTO photo (id,image) VALUES ('', '".$links."')");
?>

这应该将 img2.png 和 img5.png 插入数据库。谢谢。

【问题讨论】:

    标签: php database getimagesize


    【解决方案1】:

    循环时可以插入到数据库中

    for($i = 0; $i < $count; $i++) {
        $size = getimagesize($links[$i]);
        list($width, $height) = $size;
        if ($width > $height)
           mysql_query("INSERT INTO photo (id,image) VALUES ('', '".$links[$i]."')");
    }
    

    如果id是自增字段,可以简化sql查询

    mysql_query("INSERT INTO photo(image) VALUES('".$links[$i]."')");
    

    【讨论】:

    • 谢谢,但是如何确定尺寸宽度>=300px 和高度>=200?
    • $width=300, $height=100,是吗?
    • 谢谢。应该有双&amp;
    【解决方案2】:

    $links = array("img1.jpg", "img2.png", "img3.gif", "img4.jpg", "img5.png");
    $sizearray = array();
    $count = count($links);
    for($i = 0; $i < $count; $i++) {
        $size = getimagesize($links[$i]);
        list($width, $height) = $size;
        $sizearray[$links[$i]] = array("width" => $width, "height" => $height);
        if ($width > $height) mysql_query("INSERT INTO photo (id,image) VALUES ('', '".$links[$i]."')");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-05
      相关资源
      最近更新 更多