开发者

php web image search

开发者 https://www.devze.com 2023-02-21 05:57 出处:网络
I want to do a picture search engine. I use simple_html_dom and preg_match_all to get all the images, then use getimagesize to get all the image sizes.

I want to do a picture search engine. I use simple_html_dom and preg_match_all to get all the images, then use getimagesize to get all the image sizes. Here is one part of my code.

<?php
header('Content-type:text/html; charset=utf-8');
require_once 'simple_html_dom.php';
$v = 'http://www.jqueryimage.com/';
$html = file_get_html($v);
foreach($html->find('img') as $element) {
        if( preg_m开发者_C百科atch('#^http:\/\/(.*)\.(jpg|gif|png)$#i',$element->src)){
            $image = $element->src;
            //$arr = getimagesize($image); //get image width and height            
            //$imagesize = $arr[0] * $arr[1];  
            echo $image.'<hr />';
        }
    }
?>

First question, how to add a judgement so that I can echo the biggest size image? (only one image).

Second question, I can get the image real url in these two possibilities, first where image is as a 'http' began, second where image is as a / began. But how to get the image real url in the situation where image is as a './' or ../ or ../../ began? it is difficulty for me to judge how many ../ in a image, then cut the site url to complement a image real url?

Thanks.


Insert this before foreach loop:

$maxsize = -1;
$the_biggest_image = false;

Insert this below $image = $element->src; line

$arr = getimagesize($image);
if (($arr[0] * $arr[1]) > $maxsize) {
  $maxsize = $arr[0] * $arr[1];
  $the_biggest_image = $image;
  }

After foreach loop you'll have $the_biggest_image variable set or false if nothing found. This will take first one if more than one 'biggest image' found.

For second question, I don't know!

Edit: fixed something!

0

精彩评论

暂无评论...
验证码 换一张
取 消