cant print images (.svg) of folder using matrix - php

just print the square but not the image, dont know what is wrong, doesnt throw mistake.
Thanks
echo "<img src=\"img/$numero[$i].svg\" alt=\"$numero[$i]\" title=\"$numero[$i]\" width=\"140\" height=\"140\">\n";

Nothing wrong in your code. Check if you have correct svg image or not at particular location. (inspect using chrome developer tool)
Check using Object tag or if your browser support or not. https://www.w3schools.com/html/html5_svg.asp

I would recommend to not mix strings with code vars. Always do proper concatenation:
$str = '<img src="/img/' . $numero[$i] . '.svg"';
$str .= ' alt="' . $numero[$i] . '"';
$str .= ' title="' . $numero[$i] . '"';
$str .= ' width="140" height="140">'. "\n";
echo $str;

your code seem right, try checking if the svg image is in the correct image path you placed
i tested with this and is working well
<?php
$numero = array('imagename', 'image alt', 'title');
echo "<img src=\"Images/$numero[0].svg\" alt=\"$numero[1]\" title=\"$numero[2]\" width=\"140\" height=\"140\">\n";
?>

thank you all, I discovered that it happened
I was missing a bar in front of the img
echo "<img src=\"/img/$numero[$i].svg\" alt=\"$numero[$i]\" title=\"$numero[$i]\" width=\"140\" height=\"140\">\n";
instead of
echo "<img src=\"img/$numero[$i].svg\" alt=\"$numero[$i]\" title=\"$numero[$i]\" width=\"140\" height=\"140\">\n";
I do not see any sense

Related

echo background image with a value

I echo out an image like that:
$newString = $thumbPre.'profilemain'.$thumbPost;
echo "<img src='http://render-api-us.worldofwarcraft.com/static-render/us/" . $newString. "' alt='error'>";
Now i want the image as a background-image, i tried it like that, but it doesn´t work:
echo '<div style="background-image:url('http://render-api-us.worldofwarcraft.com/static-render/us/" . $newString. "' alt='error');"></div>';
you need to use backslashes for nested apostrophes
echo '<div style="background-image:url(\'http://render-api-us.worldofwarcraft.com/static-render/us/' . $newString.'\' alt=\'error\');></div>';
Firstly, remove alt='error' because background-image does not have an alt parameter, img does (you probably thought you could use that from your original code). In trying to use that, your background will not show up.
And your background won't show unless you have content inside that div. I've added Content as an example.
echo '<div style="background-image:url(\'http://render-api-us.worldofwarcraft.com/static-render/us/' . $newString.'\');">Content</div>';
You either have to escape the encapsulating quotes, or remove them altogether.
echo '<div style="background-image:url(http://render-api-us.worldofwarcraft.com/static-render/us/' . $newString.');">Content</div>';
Error reporting would have also thrown you a parse error such as:
Parse error: syntax error, unexpected '' alt='' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';'
http://php.net/manual/en/function.error-reporting.php
This is going to look super confusing but you need to 1, escape the quotes and 2, concatenate your path within that,
let me give you an example. I misread a bit of the question but this will serve you well moving forward especially for cleanliness sake
here is an example:
$imagePath ='PATH TO IMAGE HERE';
echo '<div style="background-image:url(\'' .$imagePath. '\')" >STUFF HERE </div>';

Unable to save an image from a remote server using PHP

OK,... this is in reference to: Copy Image from Remote Server Over HTTP
Here is my code:
for ($i = 0; $i < count($json_post['Category']); ++$i )
{
echo $json_post['Category'][$i]['CategoryID'] . '<br />';
echo $json_post['Category'][$i]['Name'] . '<br />';
echo $json_post['Category'][$i]['Image'] . '<br />';
$image_URL = "https://$_SSActiveWear_BaseURL/" . $json_post['Category'][$i]['Image'];
echo $image_URL . "<br /><br />";
copy("https://$_SSActiveWear_BaseURL/$image_URL", $_SERVER['DOCUMENT_ROOT']."/tmp/" . basename($image_URL));
die;
}
I have tried cURL with the same results.
What is happening is that the files are being created, but with all the same file length of 58k and when I attempt to open one to view it, it is unable to be opened. In fact its the HTML content of the index page from the server I am attempting to save the image from.
Edit 1
If I hard code the image to be saved instead of using the variables, it saves the correct image.
Figured this one out.
did the following change:
copy($image_URL, $_SERVER['DOCUMENT_ROOT']."/tmp/" . basename($image_URL));
I do not understand why when using a single variable, as in this case, it works and not when I use a compounded statement.
It's because your URL get interpreted wrong. Always use {} around variable in string:
copy("https://$_SSActiveWear_BaseURL/$image_URL", $_SERVER['DOCUMENT_ROOT']."/tmp/" . basename($image_URL));
is converted to copy("https://SSActiveWear_BaseUrl/_URL", ...). As you can see PHP does not find variables $_ and $image and applies null for them.
Correct syntax:
copy("https://{$_SSActiveWear_BaseURL}/{$image_URL}", "{$_SERVER['DOCUMENT_ROOT']}/tmp/".basename($image_URL));

How to correctly write <img src> in php without escaping to HTML

I am having trouble with my PHP code. I've been changing everything for 6 hours and I still get Parse errors no matter what I do. This is the code:
$slider3 = '<img src="'templates/' . $this->template . '/images/slider/slider3.jpg'">' . '" alt="' . $sitename . '" />';
The only way I can figure to not get it to throw an error is by writing it this way:
$slider3 = '<img src="templates/" . $this->template . "/images/slider/slider3.jpg" . "/>"';
but I don't think that's right.
I want $slider3 = "templates/MYTEMPLATE/images/slider/slider3.jpg" then later I will echo $slider3;
I get so confused with all the single and double quotation marks. I think the first one is right - I look at it and study it and it looks right to me. But it throws a parse error.
$slider3 = '<img src="templates/'.$this->template.'/images/slider/slider3.jpg"/>';
should work.
Explanation:
'<img src="templates/'
is a single-quoted string, which happens to contain a double-quote (which is needed for the html src attribute, or any other html attribute value really)
.
(dot) is the string concatenation operator. It concatenates ("glues") the first string together with...
$this->template
which is presumably a string containing the name of the template (not clear from your code example). Note that if $this->template comes from user input, or an otherwise unvalidated source, it could be used for cross-site scripting, eg. if it contains "><script>alert("XSS!")<script>, javascript is executed in the browser!
.
another concatenation with...
'/images/slider/slider3.jpg"/>'
which is another single-quoted string which happens to contain a double-quote, ending the src attribute value.
Try this:
$slider3 = '<img src="templates/"' . $this->template . '"/images/slider/slider3.jpg"/>';
$template = "MYTEMPLATE";
$slider3 = '<img src="templates/'.$template.'/images/slider/slider3.jpg"/>';
echo $slider3;
Will echo - >
<img src="templates/MYTEMPLATE/images/slider/slider3.jpg"/>
Just write:
<?php
$templates = "var";
echo "<img src='templates/${templates}/images/slider/slider3.jpg'/>";
it will result in
<img src='templates/var/images/slider/slider3.jpg'/>

Displaying image from within a php module

I am trying to print the image whose location is saved in my database, I have stored the absolute location in the database and not the relative one , I browsed through a lot of question including this one
include a PHP result in img src tag
I tried all the options that were given to the respective asker of the question but I didn't get my output, rest everything is being displayed apart from the image, its showing no file found
Here's my code, any help will be appreciated
while($result=#mysql_fetch_array($resul,MYSQL_ASSOC)){
$image = $result['image'];
echo $result['company'] . " " . $result['model'] . "<br>" ;
echo '<img src="$image" height="50" width="50" />';
}
I know I am using mysql functions instead of mysqli but this code is not getting live ever.
As watcher said, PHP does not do variable interpolation within single-quoted strings.
The most important feature of double-quoted strings is the fact that variable names will be expanded.
Read more about strings from the PHP manual.
Therefore, when you view the HTML, you will literally see this:
<img src="$image" height="50" width="50" />
Your code should be:
while($result = mysql_fetch_array($resul,MYSQL_ASSOC)) {
$image = $result['image'];
echo $result['company'] . " " . $result['model'] . "<br>";
echo "<img src='$image' height='50' width='50'>";
}
Alternatively, interpolate the array value:
while($result = mysql_fetch_array($resul,MYSQL_ASSOC)) {
echo $result['company'] . " " . $result['model'] . "<br>";
echo "<img src='{$result['image']}' height='50' width='50'>";
}
If the filename contains spaces or other special characters, you may need to use rawurlencode(). In this case, you must concatenate the string since you are calling a function that returns a string value:
echo "<img src='" . rawurlencode($result['image']) . "' height='50' width='50'>";
PHP will not interpolate variables when you include them within single quotes. For more information, see the manual.

Adding A Dynamic Link In Php

I have been using the following to add a dynamic link on a page I am writing, it works ok and appears how it should on the page but I cant help but think that I am going a bit backwards with the way its written as it looks messy. What is the correct way to write it, as if I put it all in one line it doesn't work ?..
echo '<a href="./customer-files/';
echo $customerID;
echo '/';
echo $filename->getFilename();
echo '">';
echo $filename->getFilename();
echo '</a>';
Try with
echo "{$filename->getFilename()}";
Here there is the documentation with a lot of examples of how to concatenate output.
I'd approach it like this:
$safe_customer_id = htmlspecialchars(urlencode($customerID));
$safe_filename = htmlspecialchars(urlencode($filename->getFilename()));
$safe_label = htmlspecialchars($filename->getFilename());
echo "$safe_label";
I would go with this:
$fn = $filename->getFilename();
$link = $customerID . '/' . $fn;
echo ''.$fn.'';
If you're using a template layer, it is even better to break out into PHP only when you need to:
<a href="./customer-files/<?php
echo $customerID . '/' . $filename->getFilename()
?>">
<?php echo $filename->getFilename() ?>
</a>
This way, your IDE will correctly highlight your HTML as well as your PHP. I've also ensured that all PHP is in single-line blobs, which is the best approach for templates (lengthy statements should be banished to a controller/script).
Concatenation is your friend. Use a . to combine multiple string expression into one.
echo ''.$filename->getFilename()/'';
Even better way would be
$filename = $filename -> getFilename(); //cache the filename
echo "<a href='/$customerId/$filename'>$filename</a>";
// ^ On this echo NOTICE that variables can be DIRECTLY placed inside Double qoutes.

Categories