简体   繁体   English

仅抓取jpg文件图像

[英]To grab only jpg file image

I have a program to show picture from my folder. 我有一个程序来显示我的文件夹中的图片。

The code: 编码:

$dir = dir("tags/carrot_cake");
while($filename=$dir->read()) {

    if($filename == "." || $filename == ".." || $filename == $first_image) continue;

    echo "<div class='cp-thumb'>";

    /**********************************sql query to fetch date time and caption************************/
     $timeqry = mysql_query("SELECT created_date FROM foodsites.images WHERE img_name='$filename'") or die(mysql_error());
     $row1 = mysql_fetch_row($timeqry);
     $datetime = $row1[0];
     $newDatetime = date('d/m/Y h:i A', strtotime($datetime));
     $capqry =  mysql_query("SELECT caption FROM foodsites.images WHERE img_name='$filename'") or die(mysql_error());
     $row2 = mysql_fetch_row($capqry);
     $caption = $row2[0];

     echo "<div class='cp-hover' style='display: none;' ><div class='cpHover-bg'></div>
            <div class='cpHover-info'><p class='text11'>".$newDatetime."</p><p class='text10'>".$caption."</p></div></div>"; 
     echo "<img src='tags/carrot_cake/".$filename."'class='img_235x235' />
     </div>";  
}

In my code, I found out I fetch all the file inside the directory includes those which are not jpg file. 在我的代码中,我发现我获取目录中的所有文件包括那些不是jpg文件的文件。

How do I change it to only grab jpg file?? 如何将其更改为仅抓取jpg文件?

You should change this line: 你应该改变这一行:

if($filename == "." || $filename == ".." || $filename == $first_image) continue;

to: 至:

if ($filename != "." && $filename != ".." && strtolower(substr($filename , strrpos($filename , '.') + 1)) == 'jpg') continue;

Another way to do that is with glob() : 另一种方法是使用glob()

$filename = glob('/tags/carrot_cake/*.jpg');

In the glob() case this returns an array containing the matched files/directories, an empty array if no file matched or FALSE on error. glob()情况下,它返回一个包含匹配文件/目录的数组,如果没有文件匹配则返回一个空数组,如果错误则返回FALSE。

Try like this 试试这样吧

while($filename=$dir->read()) {
        $allowed='jpg';  //which file types are allowed seperated by comma

        $extension_allowed=  explode(',', $allowed);
        $file_extension=  pathinfo($filename, PATHINFO_EXTENSION);
        if(array_search($file_extension, $extension_allowed))
        {
           //allowed
        }
        else
        {
            //not allowed
        }
    }
<?php
$dir = dir("tags/carrot_cake");
while($filename=$dir->read()) {

    $filename_mime = image_type_to_mime_type(exif_imagetype($filename));

    if($filename == "." || $filename == ".." || $filename == $first_image || ($filename_mime != "image/pjpeg" || $filename_mime != "image/jpeg")) continue;

    echo "<div class='cp-thumb'>";

    /**********************************sql query to fetch date time and caption************************/
     $timeqry = mysql_query("SELECT created_date FROM foodsites.images WHERE img_name='$filename'") or die(mysql_error());
     $row1 = mysql_fetch_row($timeqry);
     $datetime = $row1[0];
     $newDatetime = date('d/m/Y h:i A', strtotime($datetime));
     $capqry =  mysql_query("SELECT caption FROM foodsites.images WHERE img_name='$filename'") or die(mysql_error());
     $row2 = mysql_fetch_row($capqry);
     $caption = $row2[0];

     echo "<div class='cp-hover' style='display: none;' ><div class='cpHover-bg'></div>
            <div class='cpHover-info'><p class='text11'>".$newDatetime."</p><p class='text10'>".$caption."</p></div></div>"; 
     echo "<img src='tags/carrot_cake/".$filename."'class='img_235x235' />
     </div>";  
}
?>

Try this code 试试这个代码

<?php
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);

// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');

// Output the image
imagejpeg($im);

// Free up memory
imagedestroy($im);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM