Echo file url as image - php

I have this line of code to echo an image (thumb)
printf('<img src="./uploads/designments/%s/thumbs/80x80">',$item['designment_id']);
On the page it will show the broken image icon. and if I look in the elements I see the following path: /uploads/designments/71/thumbs/80x80"
If I look in the ftp to the corresponding file name I see here the path is:
/uploads/designments/71/thumbs/80x80/calculator_wheat_0.jpg
So what do I have to add to my code to get the corresponding filename in the URL?

You closed off the image tag a little too early. Try:
printf('< img src="./uploads/designments/%s/thumbs/80x80/',$item['designment_id'],'">');

Related

TinyMCE and RoxyFileBrowser

I am using TinyMCE and RoxyFileBrowser to create text files with images and saved them in a database.(only the html content of textarea). When I am reloading the saved content from database the images are not loading. when I checked I find out in TinyMCE html sorce code image src is incorrect. So I edit the RoxyFileBrowser conf.json as
"RETURN_URL_PREFIX": "/",
Now in the TinyMCE Insert/Edit Image window source URL is displayed as needed. But still the tinyMCE html content has wrong image src url.
Source URL=/fileman/....... - which is needed one
img src="../fileman/....." which is incorrect
What is the reason for this error?
Ok finally I found the answer.
The problem is with tinymce is trying to use relative URLs. So turn it off in tinymce.init function
relative_urls : false,
Example: "RETURN_URL_PREFIX": "http://localhost:59184/"
"RETURN_URL_PREFIX" is your domain

Random php session causes different value when i use # this in img tag like <img src="#">

In my project I have a text editor and has a Image upload specification it will upload the picture according to $_SESSION['imageID'] and $_SESSION['userID'] and when it is done it will show that picture in my editor iframe.
Note: For uploading pictures I used popup window and another php file like uploader.php.
Every ting is working well until I try the add preview section for my text editor. Then one by one I checked my preview codes and I found the problem. It's just #, in img tag src="#". It's causes different value $_SESSION['imageID'] in my uploader.php than texteditor.php.
Simply which causes this problem codes like this:
texteditor.php
<?php
session_start();
include 'function.php';
$_SESSION['imageID']=rast();
echo "Image ID :".$_SESSION['imageID'];?>
<img class="preview_image" src="#"/>
upload.php
<?php
session_start();
echo "Image ID :".$_SESSION['imageID'];
?>
For Example output like this:
From texteditor.php
Image ID :vKF8Ro6hI2VpyYX
From upload.php
Image ID :O5cg7Ro0vo882FP
Note:rast(); gives me random 15 charters from a to z, A to Z, 0 to 9 and date.
Question is Why my $_SESSION['imageID'] is causes different value in my uploader.php than texteditor.php when I use "#" this in img tag like < img src="#"/> that?
After a bit of fiddling around i finally figured it out.
When you set the image's source to be #, the browser converts that to http://www.somesite.com/texteditor.php#
This is so that if you wrote #content, the browser would scroll to the element with the id content
After the browser has converted the src tag, it tries to load the page http://www.somesite.com/texteditor.php#, because it thinks it will receive an image.
Therefore your browser loads the page twice, and your rast() function is called twice.

Change PHP GD image output file url name/link

I have this problem, i have this script in php that creates a image on the fly, the problem is that the outputted image on the browser is allright, but i need to change it's name.
Ex: Index.php
<?php $url = "http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33" ?>
<img src="<?php echo $url ?>" />
The image_scrc.php is the file that creates the image, and as you can see i have several data that is passed by the get method.
In the image_scrc.php i have tryed
header('Content-type: image/jpg');
header('Content-Disposition:inline; filename="'.$random_name_jpeg.'"');
but the html link is is always appearing like this
http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33
if i select the image on browser and then select copy image link it copies just like this also.
however, when I save the image it assumes the random_name.jpg, but only on save!
i've tried everything, even htaccess rules but nothing seems to work !!
it's this possible to acomplish? transform this
http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33
to this
http://www.somesite.com/cls/random_name.jpg
i cant have the image on the server side! and must be displayed on the fly
Thanks in advance.

li cannot find image

I have an ul with a li that cannot find a .PNG image associated with the list item 'li' in the code here:
$pngFilename= 'C:/xampp/htdocs/myProj/' . 'just_a.png';
echo 'pngFilename is "', $pngFilename, '" -- that was the .png image filename.';
// within a 'ul' is this li item that displays an image (the ul code is simplified
// to only show the item that (needs to but doesn't!) display a .PNG image.)
echo '<ul>';
echo '<li>';
echo '<a href="http://localhost/myProj/just_an.htm">';
echo '<img src="', $pngFilename, '"';
echo 'alt="http://localhost/myProj/the_other.png"';
// NOTE -- I left out the close /> to the img statement when I copied my code here but
// it was in fact in my source code.
echo '/>';
echo '</a>';
echo '</li>';
echo '</ul>';
Before I wrote the above php code, I tested just the raw html and it was fine -- my .PNG file called
C:/xampp/htdocs/myProj/just_a.png was displayed correctly.
But when I switched to php server-side generation of the html, the C:/xampp/htdocs/myProj/just_a.png image does not appear, only the 'cant find it' default small image appears that looks like a piece of paper torn horizontally.
Any ideas? The .png file exists and so does the directory and the html correctly displayed the image, but when I put the html into php 'echo' calls I must be screwing something up, just not sure what.
To make sure I have the correct path and filename you'll see I echo it out at the top of the code.
The php variable $pngFilename is displayed when I do 'View Source' in the browser as:
pngFilename is "C:/xampp/htdocs/myProj/just_a.png" -- that was the .png image filename.
The only other thing to mention is that the 'alt' link, the 'alt="http://localhost/myProj/the_other.png"' -- this link (not the image) shows as blue underlined link text.
Why did this work in my html but breaks when I use the 'echo' in php? After all, the 'echo' simply sends the html to the client side -- and that .png file is 100% definitely there and displays fine when I run the above html outside of php's "echo" command.
This is because the path to the image changes depending on how your viewing the page. in a local context or from a server context; and since you are using an absolute path instead of a Relative path the system can't adjust for the change in the location of the image. Unlike when using a PHP function that calls on the php file system functions that do use the internal file system. what your doing is having it send a text file to the browser which isn't rendered as HTML code until after PHP has finished. because of that it has no access to the php file system to resolve the path to the image on the server. the way to fix it would be to use the path to the image Relative to the PHP script or use the web accessible path to the image
$pngFilename= 'C:/xampp/htdocs/myProj/' . 'just_a.png';
Should for example be
$pngFilename= 'http://localhost/myProj/' . 'just_a.png';
or if the image and the php file are in the same directory you could just do
$pngFilename= 'just_a.png';
You should try to close your img tag:
echo '<img src="', $pngFilename, '"';
echo 'alt="http://localhost/myProj/the_other.png">';
You are generating a image tag like:
<img src="C:/xampp/htdocs/myProj/just_a.png" alt="http://localhost/myProj/the_other.png" />
You cannot use this path, you need to change it to http://localhost/myProj/just_a.png or something like file:///C:/xampp/htdocs/myProj/just_a.png.

How to retrieve image from server and display using json parsing?

I have created a form which takes in images into a folder. I have only one image per folder and I want to display the image from the specified folder.
Say I've uploaded one picture to folder name uploads. Now I want to retrieve that image from the folder and display it.
How do I do that?
When a file is uploaded with your form, it becomes a file on your server, immediately. The web server puts it in a temporary directory and PHP tells you where it was put through the $_FILES array. You can immediately access this file, you can move it somewhere within your website's documents, and you can immediately print out an <img> tag pointing to where the file was put.
Stop writing "an img tag won't work" as that is exactly how you display the image.
Read the PHP manual page "Handling file uploads":
http://php.net/manual/en/features.file-upload.php
The PHP manual should always be the first place you go when you're trying to do something you haven't done before.
<img src="/path/to/the/upload/folder/<?php echo $filename; ?>"/>
or:
echo '<img src="/path/to/the/upload/folder/'.$filename.'"/>";
First you should have the image and path (or user) names in PHP variables. Then use an IMG tag to display them like :
<img src="some_path/uploads/<?php echo $username . '/' . $imagename; ?>">
you can't do it without knowing the name of the image file.
<?php
header('Content-Type:image/jpeg');
readfile('path_to_your/image.jpg');

Categories