php web image search - php

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_match('#^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!

Related

Display random images from folder with PHP without duplicates

I am trying to display a certain amount of random images from one directory on my website without displaying duplicates.
I found this question: Display 2 random pictures PHP with no duplicates which partially answers my problem:
<?php
$pics = array('image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg');
$images = array_rand($pics, 2);
?>
<img src="images/<?php echo $pics[$images[0]]; ?>" />
<img src="images/<?php echo $pics[$images[1]]; ?>" />
The problem I have is that the images are uploaded to the folder with completely different/random names such as 1a2265fg65444.jpg 55v4423097ww6.jpg etc, so I can't manually add them to the $pics array. I need to somehow scrape the directory for *.jpg and have the array generated automatically
I did try Michal Robinsons answer on Show Random images from a folder without repeating using JS or PHP but couldn't get it to print anything for some reason:
$all_images = glob("/images/photos/{*.jpg}", GLOB_BRACE);
shuffle($all_images);
$images = array();
foreach ($all_images as $index => $image) {
if ($index == 15) break; // Only print 15 images
$image_name = basename($image);
echo "<img src='/images/photos/{$image_name}' />";
}
Perhaps I'm missing something?
Any pointers would be simply awesome.
Many thanks
try this
$scan = scandir("/images/photos/");
shuffle($scan);
$r = rand(2, count($scan)); // maybe (count($scan) - 1)
printf("<img src='/images/photos/%s' />", basename($scan[$r]));
Thanks Aron, but I managed to fix it by changing:
glob("/images/photos/ to
glob("images/photos/
To complicate things further though, I just realized I need to show 1 image from a 1st folder followed by another image from a 2nd folder, and repeat this about 5 times, still without showing any duplicate :/ This is as far as I have got, but seem to be digging a bigger hole that doesn't work, as I'm stuck at the foreach...:
$all_images1 = glob("images/photos/{*.jpg}", GLOB_BRACE);
$all_images2 = glob("images/photos1/{*.jpg}", GLOB_BRACE);
shuffle($all_images1);
shuffle($all_images2);
$images1 = array();
$images2 = array();
$class = 1;
foreach ($all_images1 as $index1 => $image1) {
if ($index1 == 10) break; // Only print 15 images
$image_name1 = basename($image1);
echo "<img src='/images/photos/{$image_name1}' class='image".$class++."' /> <img src='/images/photos1/{$image_name2}' class='image".$class++."' />";
}
Am I going in the wrong direction to do this?
Thanks Ted

How do I get the h1 inside a loop in simple html dom?

How do I get the h1 inside a loop in simple html dom?
This is the code that I'm using to fetch images and save them in a folder.
It works fine.
But I don't have any idea on how to save h1 as file name
include("simple_html_dom.php");
$files = file_get_contents("http://picjumbo.com/page/2/");
$html = new simple_html_dom();
$html->load($files);
//Get all the item-wrap divs
foreach($html->find('div[class=item_wrap]') as $item_wrap){
//fetch each image and save it
foreach($item_wrap->find('img[class=image]') as $img){
$img_src = explode('/',$img->src);
$img_src = explode('-',$img_src[5]);
$img = $img_src[0];
$url = 'http://picjumbo.com/wp-content/themes/picjumbofree/run.php?download&d='.$img.'.jpg';
copy($url, 'images/'.$img.'.jpg');
}
}
I tried this but it is not working.I can't get my head around it
foreach($html->find('div[class=item_wrap]') as $item_wrap){
foreach($item_wrap->find('img[class=image]') as $img){
$img_src = explode('/',$img->src);
$img_src = explode('-',$img_src[5]);
$img = $img_src[0];
//this throws an error (I know a foreach is required but how do I save h1 as file name with it?)
$h1 = $item_wrap->find('h1')->plaintext;
$url = 'http://picjumbo.com/wp-content/themes/picjumbofree/run.php?download&d='.$h1.'.jpg';
copy($url, 'images/'.$img.'.jpg');
}
}
You get the idea right?I just want to save the image as h1.
Need your help!!!
Find('...', $index) returns the Nth element specified by $index (zero based), and If $index is not set it returns an array...
So, since you know there's only one h1 in each block, then it willl correspond to the first element in the array returnd by find():
$h1 = $item_wrap->find('h1', 0)->plaintext;
You can also try:
$h1Temp = $item_wrap->find('h1');
$h1 = $h1Temp[0]->plaintext;
Edit: Damn, I see you already got similar answer in the comments, i'll leave it anyway here, it may help others..

How to batch add images with similar name using jquery/ php

I'm adding images to a website but the amount I want to add would take a long time, is there a quicker way of adding these images and for it to automatically find new images if the file name was appended +1 such as image1.jpg, image2.jpg etc.
I am using PHP. Also thought maybe this could be done using a javascript or jquery loop, for loop maybe, im just unsure how.
<img src="images/other/pic2.png"></a>
<img src="images/other/pic3.png"></a>
<img src="images/other/pic4.png"></a>
<img src="images/other/pic5.png"></a>
<img src="images/other/pic6.png"></a>
<img src="images/other/pic7.png"></a>
<img src="images/other/pic8.png"></a>
Its not a good idea to blindly loop a curtain amount of iterations hoping that the file is there and is a valid file, a better way would to loop the directory of files, check that the file is first a valid image and contains the expected filename prefix. This way you can add as may images to the dir knowing that there be added without changing code.
<?php
$img_path = './images/other/';
$prefix = 'img';
if ($fh = opendir($img_path)) {
while (false !== ($file = readdir($fh))) {
if ($file != "." && $file != "..") {
//Validate its an image and get size, also check that the image filename starts with the $prefix
$attr = getimagesize($img_path.$file);
if(isset($attr[3]) && substr($file,0,strlen($prefix)) == $prefix){
echo '<img src="'.$img_path.$file.'" '.$attr[3].' alt="'.$file.'"/>';
}
}
}
closedir($fh);
}
?>
Well judging by your description, doing something like this would probably be suitable:
$initialImageNumber = 2;
$endingImageNumber = 9;
for ($i = $initialImageNumber; $i <= $endingImageNumber; $i++)
echo '<img src="images/other/pic' . $i . '.png">';

Selecting random photos from featured album, PHP

Greetings,
I am adding four random photos from a featured photo album to the front page of a site in the form of a jQuery photo slider. Since all of the images must be the same size, I select only horizontal photos, shuffle them, and then trim that down to 4. Here is the code I am using. My question - is there a simpler, perhaps more efficient way to do this? Or is my method fairly sound?
Thanks!
$getImages = $gallery_db->query("SELECT * FROM images WHERE album = '5'"); //sample SQL
$imagesArr = array();
while ($image = $getImages->fetch()) {
$path = "http://somewhere.com/gallery/photos/" .
$image['album'] . "/" . $image['filename'] . ".jpg"; //All files are .jpg
list ($width, $height) = getimagesize($path);
if ($width > $height) {
$imagesArr[] = $path;
}
}
shuffle($imagesArr);
array_splice($imagesArr, 4)
and then, to output:
foreach ($imagesArr as $path) {
echo "<img src=\"$path\" width=\"220\" height=\"110\"/><br/>\n";
}
Your solution looks just fine, it's pretty simple and straight forward. But remember, premature optimization is the root of all evil :)
A improvement would be to store the dimensions of each image in your database, so that you can fetch all images of a certain size and just take 4 random images.

How do I list imags from directory?

I'm trying to show all images within a specified directory.
The following code lists all allowed file names:
function getDirectoryList()
{
// create an array to hold directory list
$results = array();
// create a handler for the directory
$handler = opendir($this->currentDIR);
// open directory and walk through the filenames
while ($file = readdir($handler)) {
// Make sure we get allowed images types
if ($this->allowedFileType($file,$this->allowedImageTypes) )
{
$results[] = $file;
}
}
// tidy up: close the handler
closedir($handler);
// done!
return $results;
}
(...)
$images = getDirectoryList();
foreach($images as $img) {
echo "<li>".$img."</li>";
}
How can I get file size and MIME type?
I read that mime_content_typeis deprecated and I should use finfo_file istead. But I've not been very successfull with this.
Do I have to use /usr/share/misc/magic to get file information? Can't I use GD library?
I've looked at many examples, but they are old and don't work that well.
Any help appreciated.
to get the size and mime type of image its simple,
use function : getimagesize
uses like :
list($width, $height, $type, $attr) = getimagesize("img/myimg.jpg");
Returns an array with 7 elements.
Index 0 and 1 contains respectively the width and the height of the image.
Index 2 is one of the IMAGETYPE_XXX constants indicating the type of the image.
using filesize give the size in bytes
To expand on Haim Evgi's post, use getimagesize() to retrieve the dimensions and the image type in an array. Then, use image_type_to_mime_type() on the image type code to retrieve the MIME:
list ($fileWidth, $fileHeight, $fileType) = getimagesize($filename);
$fileMimeType = image_type_to_mime_type($fileType);

Categories