I have the following code for a gallery. When the thumbnail is clicked, I would like to open the big image in another window (which it does) and then be able to navigate to the next image in the new window, but in order to do this, I need to strip the .jpg off the filenames so that I can just '+1' to the filename, as they are sequentially numbered.
For example, files are numbered 001.jpg, 002.jpg, 003.jpg etc.
I currently have:
echo '<p>basename=' . basename($i) . '</p>';
Which give the name of the file with .jpg extension, for example basename=001.jpg.
Then I have :
$image = basename($i); // to give the variable $image
$img = str_replace('.jpg',$image, ''); //to take off the .jpg extension
but the output I expect, 001, doesn't echo. It just has nothing... img=
echo '<p>img=' . $img . '</p>';
What am I doing wrong? Can anyone point me in the right direction?
Many thanks, Kirsty
Try this
$YourPicture = 'cat.jpg';
$without_extension = pathinfo($YourPicture, PATHINFO_FILENAME);
result will be
cat
Related
I have used a function
/*
Random File Function
Written By: Qassim Hassan
Website: wp-time.com
Twitter: #QQQHZ
*/
function Qassim_Random_File($folder_path = null){
if( !empty($folder_path) ){ // if the folder path is not empty
$files_array = scandir($folder_path);
$count = count($files_array);
if( $count > 2 ){ // if has files in the folder
$minus = $count - 1;
$random = rand(2, $minus);
$random_file = $files_array[$random]; // random file, result will be for example: image.png
$file_link = $folder_path . "/" . $random_file; // file link, result will be for example: your-folder-path/image.png
return '<img src="'.$file_link.'" alt="'.$random_file.'">';
}
else{
return "The folder is empty!";
}
}
else{
return "Please enter folder path!";
}
}
?>
to pull a random image from a specific wordpress directory. I can successfully call up a random image file however it does not display, just displays the image filename and a broken image icon.
The code I am using from the tutorial to display the image is <?php echo Qassim_Random_File("my-folder-name"); // display random image! ?>
I am brand new to php as of this week, and a fairly novice coder in general (know some basics) so I really am stumped. I've fooled with other possible solutions from stack overflow already but this tutorial is the only thing I've been able to get close to working. Many thanks if anyone can spot the problem!
This is showing up in the HTML inspector:
<div class="elementor-widget-container">
<h5>call</h5>
<a href="wp-content/uploads/H-PH-MA-Greyscale/image_2.jpg" target="_blank" title="image_2.jpg">
<img src="wp-content/uploads/H-PH-MA-Greyscale/image_2.jpg" alt="image_2.jpg">
</a>
</div>
You just need to replace this line:
$file_link = $folder_path . "/" . $random_file;
...with this one:
$file_link = get_site_url(null, $folder_path . "/" . $random_file);
What was the problem?
scandir is returning the physical file path, not the url to the file - we can confirm this is we look at one of the values it returns for $random_file, e.g. wp-content/uploads/H-PH-MA-Greyscale
This is a relative URL, but we need an absolute URL so that it will work no matter where it is called. We also need to include the site URL so that the path to wp-content is correct. (Note: site URL and home URL can be different - the site URL is the one that always point to the location of the WP files).
Another problem is the trailing slash (or lack of one) on the site URL. get_site_url and get_home_url do not add the trailing slash to the URL, so you need to add it yourself if it isn't included in the file path.
So how do we solve this?
Using the get_site_url function added the correct path to the WP files, and it also lets us pass it the file path with our without a preceding slash as a parameter, and it will add the slash on the path only if it is needed.
The code looks simple, if the folder exist and it has more then 2 file (it considers the "." and ".." and files when you use scandir) then you should be able to see something. What's the generated HTML code for that function call?
Be aware that the code you are using builds the file url using the phisical path, that isn't going to work. You should fix this part:
if( $count > 2 ){ // if has files in the folder
$minus = $count - 1;
$random = rand(2, $minus);
$random_file = $files_array[$random]; // random file, result will be for example: image.png
$file_link = get_home_url().$folder_path . "/" . $random_file; // file link, result will be for example: your-folder-path/image.png
return '<img src="'.$file_link.'" alt="'.$random_file.'">';
}
Notice the get_home_url() to build the real URL which is needed for your <img tag src
I have a grid of thumbnails that when clicked on I want to load an overlay on the page showing the full size image and the contents of a text file as a caption.
Something like
<image src="thumbs/1001.jpg"> would just load the image, but I want to load the image in a light box sort of view and also load the contents to "desc/1001.txt" into a caption (not necessarily a <caption>) below the image.
I don't want it to reload the page if that can be avoided. If it helps, the images, thumbnails, and description all match the pattern above, or I also have a CSV file in the format of 1000,"Description of the picture" if that is simpler.
If you use something like https://lokeshdhakar.com/projects/lightbox2/#getting-started this should be fairly easy.
Your PHP code would look something like this:
<?php
$photos = [1000,1001];
foreach ($photos as $photo) {
$description = file_get_contents('desc/' . $photo . '.jpg');
echo '' . $photo . '';
}
?>
Using the code below with the Sphider search engine, Sphider will display an image in search results - if the html page and jpg share the same name (i.e) page14.html, and page14.jpg.
1) Without knowing the name of the image, how could the code be modified so that the first of 2-3 jpg's is displayed from the html page returned in search results ?
<?PHP
$url_path = "http://www.webpage.com/"; // The url to indexed pages.
$url_ext = ".html"; // The file ext. of indexed pages.
$image_path = "../"; // Path to image folder.
$image_ext = ".jpg"; // The file extension of images.
$image = str_replace("$url_path", "$image_path", $url);
$image = str_replace("$url_ext", "$image_ext", $image);
if (file_exists($image)) { // Do nothing
} else { $image = $image_path . "ICS" . $image_ext; } ?>
<img src="<?PHP print $image ?>" align="left" style="margin-right:10px;" />
Or,
2) How would I modify the above php code to display the images below in search results - without renaming all the images ?
I have many gallery pages with hundreds of the following html image links;
<img SRC="chair_0.jpg">
<img SRC="table_0.jpg">
<img SRC="bed_0.jpg">
In my case, all our webpage images have the same ID as the end of each page.
Following is an example of how our pages look like: www.mywebsite.com/category/sub-category/etc/details.php?category=6&subcategory=s&sc=180&productid=98
The pictures are located in another path, and share the same last digits (i.e 98) as web page.
Following is an example of picture folder: www.mywebsite.com/admin/picures/
In addition, all pictures have an extra "p" at the front and "_full" at end of the digits, as following example "p98_full.jpg", p109_full.jpg" or "p3_full.jpg".
Following method worked for me. You will need to past this on sphider/templates/standard/search_results.html line # 76, modify it a bit to match your setting and see if it works... (You do not need to add a url link to your site to this code, this should work as is)
<?PHP
$url_ext = ".php"; // The file extension of indexed pages.
$image_path = "../admin/pictures/"; // Path to image folder.
$pic_name = "p"; // Alphabet before image, if any.
$page_end = Substr ($url2, strrpos($url2, "=")+1); // Isolate digits or alphabets starting from end of page.
$image_ext = "_full.jpg"; // The file extension of images.
$image = $image_path . "$pic_name$page_end" . $image_ext; // Select isolated digits or alphabes.
if (file_exists($image)) { // Do nothing
} else { $image = $image_path . "ICS" . $image_ext; } ?>
<img src="<?PHP print $image ?>" alt="" align="left" style="margin-right:10px; border:0;" />
based on the original code in this question and Tims correction over there (answer 1) I'm trying to achieve the following:
I have a folder "img" containing images as follows:
image_123.jpg
image_123_alternate.jpg
image_456.jpg
image_456_alternate.jpg
image_789.jpg
image_789_alternate.jpg
and so on...
(Note: all images have the same size of 200px/200px)
When pulling from the "img" folder the simple HTML page should display images and filenames in the following fashion:
The actual images that contain the "_alternate" part in their filename (and only these) + the image filename not containing the "_alternate" part (without the file-extension) underneath the image in a textbox. All pairs should be pulled in an alphabetical order. In the example below bold capital letters indicate the actual image to be displayed:
IMAGE_123_ALTERNATE
textbox: "image_123"
IMAGE_456_ALTERNATE
textbox: "image_456"
IMAGE_789_ALTERNATE
textbox: "image_789"
This is what I have so far but this displays all images and filenames:
<?php
$dir = opendir("img");
while (($file = readdir($dir)) !== false)
{
echo "<a href='img/".$file."' target='_blank'> <img src='img/".$file."'width='200' height='200' /></a><br />";
echo "<textarea>$file</textarea><br /><br />";
}
closedir($dir);
?>
Any help would be much appreciated!
Thanks a lot!
This code should display a list of all images in alphabetical order (using PHP's GLOB function).
It then outputs a HTML image (leading to the "_alternate" image) and the image's filename (without the path, "_alternate" or "extension").
<?php
foreach (glob("img/*_alternate.jpg") as $file) {
$filename_parts = pathinfo($file);
$filename = $filename_parts['filename'];
$filename = str_replace("_alternate", "", $filename);
echo "<div>";
echo "<img src=\"/$file\" width=\"200\" height=\"200\" />";
echo "<p>$filename</p>";
}
?>
I have broken apart the functions that find the new filename and the parts that output the HTML code to make it easier to read and understand, you could merge these into one function if you wanted to.
Just through an if statement in your while loop. Check if the file name contains alternate or or not. If it doesn't contain alternate run your same code replacing the file extension with "_alternate.ext" while keeping the text area the same. If it does contain alternalte, do nothing. That way you are only processing half the files.
$files = new \DirectoryIterator('img'); // <- this should be the full absolute path!
$images = array();
// pick up jpg images that end in 'alternate'
foreach($files as $file){
$name = $file->getBasename('.jpg');
if(strripos($name, 'alternate') === (strlen($name) - 9))
$images[] = $name;
}
// sort them
sort($images, SORT_STRING);
// and do your thing here with $images...
var_dump($images);
with the code below I get an image through a form on a html page, make some cuts in it and add another image to it, it's working perfectly.
I'm having problems with special characters in the name of the image that I got from the html form ...
In short, before you upload, crop images, move them, etc., I must treat their name ... removing blank spaces, remove accents, etc. ..
I tried to use some functions but without success .. Can someone give a help?
Here my code:
<?php
require( "./lib/WideImage.php");
// Example of accessing data for a newly uploaded file
$fileName = $_FILES["uploaded_file"]["name"];
$fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"];
// Path and file name
$pathAndName = "cartelas/cart".$fileName;
// Run the move_uploaded_file() function here
$moveResult = move_uploaded_file($fileTmpLoc, $pathAndName);
// Evaluate the value returned from the function if needed
$image = WideImage::load($pathAndName);
$unh = WideImage::load("unh11.png");
$crop1 = $image->crop("25", "50", 111, 132);
$out1 = $crop1->merge($unh,'middle','middle');
$pathAndName1 = "unha-1-".$fileName;
$crop1->saveToFile('./cartelas/estampa'.$pathAndName1);
$out1->saveToFile('./cartelas/'.$pathAndName1);
echo "Imagens geradas:<BR>";
echo "<img src=./cartelas/estampa$pathAndName1><img src=./cartelas/$pathAndName1>";
?>
Thank you!
if u want to do it on php side you can do whatever you want with a filename. If you don't have to have exact same name you can use rawurlencode() or base64_encode(); or even md5() hash from the name. This should solve problems with weird names or special chars.
If u need store all this files u can do $newfilename = md5($filename) . '_' . uniqid(); so they will be unique. You can add also user id or something like that.
imclickingmaniac it would lose its extension if you do that! so instead do...
<?php
$ext=pathinfo($_FILES["file1"]["name"], PATHINFO_EXTENSION);
$fileName=md5($fileName);
$fileName="$fileName.$ext";
?>