How to determine the picture size, then selectively insert into the database? php
I have 5 pictures, img1.jpg, img2.png, img3.gif, img4.jpg, img5.png.
How do I determine the picture size, if it is a Horizontal picture( width is more than height)width>=300px and height>=200, then insert into the database?
Else the picture is a Vertical picture or width less than 300px and height less than 200px, don't insert it into the database?
<?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开发者_JS百科.png] => Array ( [width] => 400 [height] => 300 ))
?>
<?php
mysql_query("INSERT INTO photo (id,image) VALUES ('', '".$links."')");
?>
This should insert img2.png and img5.png into the database. Thanks.
When you cycle, you can insert into the database
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]."')");
}
If the id is an autoincrement field you can simplified the sql query
mysql_query("INSERT INTO photo(image) VALUES('".$links[$i]."')");
$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]."')");
}
精彩评论