opening an image on a new tab - php

hey so i'm running into the problem that i want to make a image open on a new tab, but with the way i open images into the code i don't know how to do it
<?php
$all_files = glob("commissions/*.*");
for ($i=0; $i<count($all_files); $i++)
{
$image_name = $all_files[$i];
$supported_format = array('gif','jpg','jpeg','png');
$ext = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
if (in_array($ext, $supported_format))
{
echo ' <img class="comimg" src="'.$image_name .'" alt="'.$image_name.'" />';
} else {
continue;
}
}
?>

just put the image''
if (in_array($ext, $supported_format))
{
echo ' Image Name Here';
} else {
continue;
}
or
if (in_array($ext, $supported_format))
{
echo ' <img class="comimg" src="'.$image_name .'" alt="'.$image_name.'" />';
} else {
continue;
}

You need a new page to display your images for example show-image.php
<?php
if(isset($_GET['image']) && file_exists("commissions/".$_GET['image'])) {
$image_name = $_GET['image'];
$supported_format = array('gif','jpg','jpeg','png');
$ext = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
if (in_array($ext, $supported_format))
{
echo '<img class="comimg" src="'.$image_name .'" alt="'.$image_name.'" />';
} else {
echo "no preview available";
}
} else echo "image not found";
then change your links to point to that page
echo ' <img class="comimg" src="'.$image_name .'" alt="'.$image_name.'" />';

Related

Sort array by time only on image echos

I'm trying to sort these echo results by using only time, and I can't seen to find a way to read when was the image placed on the folder (to sort them by the specific time).
<?php
$all_files = glob(DIR_PATH."*.*");
for ($i=0; $i<count($all_files); $i++)
{
$image_name = $all_files[$i];
$supported_format = array('gif','jpg','jpeg','png');
$ext = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
if (in_array($ext, $supported_format))
{
echo '<div class="col" ><img src="/led/images/autoplay/'.str_replace(DIR_PATH,'',$image_name) .' " /><div align="center"><br />Delete</div><br /></div>';
} else {
continue;
}
}
?>
Maybe using usort() and filetime() function:
$files = glob(DIR_PATH."*.*");
usort( $files, function( $a, $b ) {
return filemtime($a) - filemtime($b);
} );
foreach ($files as $file) {
$image_name = $file;
$supported_format = array('gif','jpg','jpeg','png');
$ext = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
if (in_array($ext, $supported_format))
{
echo '<div class="col" ><img src="/led/images/autoplay/'.str_replace(DIR_PATH,'',$image_name) .' " /><div align="center"><br />Delete</div><br /></div>';
} else {
continue;
}
}

Display images from a directory and link to video with same name

I'm using below code to show all jpg's within a directory.
$dirname = "var/www/media/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
In the same directory I have movies with the same name. Example:
ls var/www/media
Aliens.mpg
Aliens.jpg
Simsons.avi
Simsons.jpg
I need each jpg image to be clickable, linking to the corresponding video file.
Is there an easy way doing that?
Here you go!
NOTE
1) Make sure file names are same.
2) You can add more ext file in the variables $imgExts and $vidExts.
<?php
$files = glob("media/*.*");
$vid = NULL;
$imgExts = array("gif", "jpg", "jpeg", "png", "tiff", "tif");
$vidExts = array("mp4", "mpg", "avi", "mk4", "ogg", "3gp");
for ($i=0; $i<count($files); $i++) {
$image = $files[$i];
$urlExt = pathinfo($files[$i], PATHINFO_EXTENSION);
if (in_array($urlExt, $imgExts)) {
for ($j=0; $j<count($files); $j++) {
$urlExt2 = pathinfo($files[$j], PATHINFO_EXTENSION);
if (in_array($urlExt2, $vidExts)) {
if (strcmp($urlExt, $urlExt2) == 0) {
$vid=$files[$j];
}
}
}
echo '<img src="'.$image .'" />'."<br />";
}
}
UPDATE:
It will show the error if any file not found(i.e. if image not found for video or vice versa)
<?php
$x=1; // initially giving value for x=1
$files = glob("media/*.*");
$vid=NULL;
$vidf=$files; //making copy of files array
$imgExts = array("gif", "jpg", "jpeg", "png", "tiff", "tif");
$vidExts = array("mp4", "mpg", "avi", "mk4", "ogg", "3gp");
for ($i=0; $i<count($files); $i++) {
$image = $files[$i];
$urlExt = pathinfo($files[$i], PATHINFO_EXTENSION);
if(in_array($urlExt,$imgExts )){
for($j=0; $j<count($files); $j++){
$urlExt2 = pathinfo($files[$j], PATHINFO_EXTENSION);
if(in_array($urlExt2,$vidExts )){
if(strcmp(pathinfo($files[$i], PATHINFO_FILENAME),pathinfo($files[$j], PATHINFO_FILENAME))==0){
$vid=$files[$j];
$x=0; // put the value of x=0 if video for that image found!
unset($vidf[array_search($vid , $vidf)]); // search & delete video from array with have images
}
}
}
if($x==0)
echo '<img src="'.$image .'" />'."<br />";
else if($x==1){ //check if image have the video
echo 'Video for Image <b>', pathinfo($image , PATHINFO_FILENAME),'.',pathinfo($image , PATHINFO_EXTENSION), ' </b>Not Found!<br>';
$x=0;}
}
}
foreach ($vidf as $vidf) { // show let out videos who's images not found
$urlExt2 = pathinfo($vidf, PATHINFO_EXTENSION);
if(in_array($urlExt2,$vidExts )){
echo "Image of the Video <b> ",pathinfo($vidf , PATHINFO_FILENAME),'.', pathinfo($vidf , PATHINFO_EXTENSION),'</b> Not found!' ;
echo '<br>';
}
}
?>

Displaying image name below the images in php

This is the code I am using for making a gallery type:
Main PHP :
<?php
$folder_path = 'images/'; //image's folder path
$num_files = glob($folder_path . "*.{JPG,jpg,gif,png,bmp}", GLOB_BRACE);
$folder = opendir($folder_path);
if($num_files > 0)
{
while(false !== ($file = readdir($folder)))
{
$file_path = $folder_path.$file;
$extension = strtolower(pathinfo($file ,PATHINFO_EXTENSION));
if($extension=='jpg' || $extension =='png' || $extension == 'gif' ||
$extension == 'bmp')
{
?>
<a href="<?php echo $file_path; ?>"><img src="<?php echo $file_path; ?>"
height="200" /></a>
<?php
}
}
}
else
{
echo "the folder was empty !";
}
closedir($folder);
What should I do to display all image-names. Please help.
Try this inside while loop:
$file_path = $folder_path.$file;
$path_parts = pathinfo($file_path);
$extension = strtolower($path_parts['extension']);
if($extension=='jpg' || $extension =='png' || $extension == 'gif' || $extension == 'bmp')
{
echo '<img src="'.$file_path.'" height="200" />'.$path_parts['filename'].'';
}
Check this line:
$file_path = $folder_path.$file;
here $file is the name of the file, you can use this to display the file name like:
echo $file;
As per your requirement it is like:
<a href="<?php echo $file_path; ?>"><img src="<?php echo $file_path; ?>"
<?php echo $file; ?>

Multiple image upload with PHP

I'm trying upload multiple images. Below you can see my code.
Image uploaded massage repenting (exactly the amount of image chosen to upload.)
How can i show "Image uploaded" massage only ones on successful submit?
If i put the message after the loop it will start to show no matter if there is any errors.
This is my PHP code:
<?php
error_reporting(0);
session_start();
include('db.php');
$id = $mysqli->escape_string($_GET['id']);
define ("MAX_SIZE","9000");
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$valid_formats = array("jpg", "png", "gif", "jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$uploaddir = "gallery/"; //a directory inside
foreach ($_FILES['photos']['name'] as $name => $value)
{
$filename = stripslashes($_FILES['photos']['name'][$name]);
$size=filesize($_FILES['photos']['tmp_name'][$name]);
//get the extension of the file in a lower case format
$ext = getExtension($filename);
$ext = strtolower($ext);
if(in_array($ext,$valid_formats))
{
if ($size < (MAX_SIZE*1024))
{
$image_name=time().$filename;
$newname=$uploaddir.$image_name;
if (move_uploaded_file($_FILES['photos']['tmp_name'][$name], $newname))
{
$mysqli->query("INSERT INTO galleries(image) VALUES('$image_name')");
echo "Image uploaded";
}
else
{
echo '<span class="imgList">You have exceeded the size limit! so moving unsuccessful! </span>';
}
}
else
{
echo '<span class="imgList">You have exceeded the size limit!</span>';
}
}
else
{
echo '<span class="imgList">Unknown extension!</span>';
}
}
}
?>
Any help will be appropriated.
You can implement a counter and when an upload completes successfully you can increment that counter variable then compare it against total number of array items after foreach loop completes. I modified your code for this (haven't checked it but should work).
<?php
error_reporting(0);
session_start();
include('db.php');
$id = $mysqli->escape_string($_GET['id']);
define ("MAX_SIZE","9000");
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$valid_formats = array("jpg", "png", "gif", "jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$uploaddir = "gallery/"; //a directory inside
$successfulUploads = 0;
foreach ($_FILES['photos']['name'] as $name => $value)
{
$filename = stripslashes($_FILES['photos']['name'][$name]);
$size=filesize($_FILES['photos']['tmp_name'][$name]);
//get the extension of the file in a lower case format
$ext = getExtension($filename);
$ext = strtolower($ext);
if(in_array($ext,$valid_formats)) {
if ($size < (MAX_SIZE*1024)) {
$image_name=time().$filename;
$newname=$uploaddir.$image_name;
if (move_uploaded_file($_FILES['photos']['tmp_name'][$name], $newname)) {
$mysqli->query("INSERT INTO galleries(image) VALUES('$image_name')");
//echo "Image uploaded";
$successfulUploads = $successfulUploads + 1;
} else {
echo '<span class="imgList">Moving unsuccessful! </span>';
}
} else {
echo '<span class="imgList">You have exceeded the size limit!</span>';
}
} else {
echo '<span class="imgList">Unknown extension!</span>';
}
}
if($successfulUploads === count($_FILES['photos'])){
echo 'UPLOAD SUCCESS!';
} else {
echo 'NOT ALL IMAGES WERE UPLOADED SUCCESSFULLY';
}
}
*If you wanted to get more complex with it you could create another array variable instead of a counter and if the upload fails you could add the file name to the array and then check the length of the array where I'm doing the comparison. If count > 0 then you would know there was an error and you could echo the filenames that failed to upload

automatic image gallery insert

I have this code set up for inserting any image that is uploaded to my "images" folder, right into a gallery I have.... My problem is that it kind of inserts them randomly.. I would like to set it up to insert the most recently uploaded picture to the end of the gallery "row", any suggestions? Thanks
<?php
$image_dir = 'uploads/images/';
$per_column = 10;
if ($handle = opendir($image_dir)) {
while (false !== ($file = readdir($handle)))
{
if ($file != '.' && $file != '..')
{
if(strstr($file,'.png'))
{
$files[] = $file;
}
if(strstr($file,'.jpg'))
{
$files[] = $file;
}
if(strstr($file,'.gif'))
{
$files[] = $file;
}
if(strstr($file,'.jpeg'))
{
$files[] = $file;
}
}
}
closedir($handle);
}
if(count($files))
{
foreach($files as $file)
{
$count++;
echo '<li><img src="',$image_dir,$file,'" width="20" height="20" title="',$file,'"/></li>';
if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
}
}
else
{
echo 'no pictures yet...';
}
?>
<?php
$image_dir = 'uploads/images/';
$per_column = 10;
$validExt = array(
'png' => 'image/png',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpg',
'gif' => 'image/gif',
);
if ($handle = opendir($image_dir)) {
while (false !== ($file = readdir($handle)))
{
$ext = strtolower(substr($file, -3));
if (isset($validExt[$ext]))
{
$stats = stat($image_dir.$file);
$files[$stats['mtime']] = $file;
}
}
closedir($handle);
}
$count = 0;
krsort($files);
$cnt = count($files);
if($cnt)
{
foreach($files as $file)
{
$count++;
echo '<li><img src="' . $image_dir . $file . '" width="20" height="20" title="' . substr($file, 0, -4) . '"/></li>'.chr(10);
if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
}
}
else
{
echo 'no pictures yet...';
}
I'm not sure what your file format is, but try setting the filename to the current time using time() when uploading.
When you list your images, try using glob() instead of readdir.
Your code might then go as follows (untested):
$image_dir = 'uploads/images/';
$per_column = 10;
$files = glob($image_dir . "*.jpg|png|gif|jpeg");
if(count($files))
{
foreach($files as $file)
{
$count++;
echo '<li><img src="',$image_dir,$file,'" width="20" height="20" title="',$file,'"/></li>';
if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
}
}
else
{
echo 'no pictures yet...';
}
Coupling this with giving the uploaded file a filename of the current time, this should order your images by most recent.
The most important line here is $files = glob($image_dir . "*.jpg|png|gif|jpeg");. This will scan the directory for any file (*) ending in .jpg, .png, .gif or .jpeg. You don't need to worry about . and ..; the expression filters that out.

Categories