Change part of URL string for images with PHP - php

In my MySQL database, the image links are saved in the format like http://www.old.com/image1.jpg. But I had to change the domains of the images and the new links appear like images.new.com/image1.jpg. I have been changing the images links with jQuery with the following function:
$(document).ready(function() {
$('img').each(function() {
var src = $(this).attr('src');;
$(this).attr('src', src.replace('www.old', 'images.new'));
});
});
But I am wondering is there any way to change part of the URL strings with PHP. I am getting the image URLs with the following PHP function.
<?php
$imageQuery = $mysqli->query("SELECT imageURL FROM images WHERE album = 'UK' ORDER BY date ASC");
$images = $imageQuery->fetch_all(MYSQLI_ASSOC);
$images = array_chunk($images, 2);
?>
<div class="row">
<div class="col-4" id="box1">
<?php foreach (array_column($images, 0) as $image): ?>
<img class="img-fluid" src="<?= $image['imageURL']; ?>">
<?php endforeach; ?>
</div>
<div class="col-4" id="box2">
<?php foreach (array_column($images, 1) as $image): ?>
<img class="img-fluid" src="<?= $image['imageURL']; ?>">
<?php endforeach; ?>
</div>
</div>
With PHP how can I echo the new links for the images directly here in the img src? <img class="img-fluid" src="<?= $image['imageURL']; \\modified link here ?>">

PHP has it's own method for replacing strings, str_replace. The equivalent to your jQuery in PHP is:
str_replace('www.old', 'images.new', $image['imageURL'])
A better idea would be to update the values in your database.
An even better idea would be to not store the duplicate root URLs anywhere, and stitch them together in the application if you need absolute URLs, for some reason.

You could alternatively convert the text in the sql select statement:
SELECT REPLACE(imageURL, 'www.old', 'images.new') AS imageURL FROM ...
By using AS imageURL none of the rest of the code would need to change.

Related

Problem displaying images saved as blob images

am having an issue displaying images stored as blob in my database, as the images doesn't display rather is shows crazy characters and symbols, i tried to change the tags for the php but with no avail. Help and here is my code: Note that i want all types of images to be saved as blob (jpg, jpeg, gif, svg, png) - thanks in advance
<?php
include 'include/connect.php';
$sql = "SELECT * FROM room_details ORDER BY id ASC LIMIT 2, 1;";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
?>
<div class="room-thumb"><?php echo '<img alt="room 3" class="img-responsive" src="data:image;base64,'.$row['image'].'">'; ?>
<div class="mask">
<div class="main">
<h5><?= $row['room_type']; ?></h5>
<div class="price"><?= $row['room_name']; ?><span>a night</span></div>
</div>
Read More
</div>
</div>
} ?>
Since the blob url is decoded with base64, you need to encode it first. You can use either JavaScript or PHP to do this.
JavaScript:
With JavaScript, you can just use the atob() function to encode your base64 url like this:
<script>
var x = <?php echo $row['image'] ?>
document.getELementById("code").innerHTML = atob(x);
</script>
<div class="room-thumb"><?php echo '<img alt="room 3" class="img-responsive" src="data:image;base64,' ?><span id="code"></span><?php echo '">'; ?>
<!-- DIV CONTENT HERE -->
</div>
However, as stated in this other SO thread, it would be a better approach if you use PHP itself or other JavaScript approaches (mentioned in that same thread) to retrieve and encode the base64 url as the above approach is not secure.
PHP:
With PHP, you can either encode the base64 url before outputting it into your HTML like this:
<?php echo '<img alt="room 3" class="img-responsive" src="data:image;base64,' . base64_encode(.$row['image']).'">'; ?>
Or you can create a new php file, encode the PHP in that file and then echo the encoded base64 url to your HTML like this:
<!-- PHP file -->
<?php
$id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
$image = base64_encode(getImageFromDatabase($id)); // your code to fetch the image
header('Content-Type: image/jpeg');
echo $image;
?>
<!-- HTML -->
<img src="image.php?id=<?php echo $image_id; ?>" />
Check out the accepted answer in this other SO thread for a more in-depth explanation of the above two PHP approaches.

FuelPHP- Using Asset::img() with images fetched from database by for loop

<?php foreach($works as $work) : ?>
<?php echo Asset::img('project-icons/icon/$work->cover_img', array('class'=>'img-responsive', 'alt'=>'...')); ?>
<?php endforeach; ?>
I am using FuelPHP to build a portfolio website. PHP v5.6
This is the "works" section where images and details of a "work" are fetched from database. Using foreach loop. I want to get the "cover_img" (which is the image name), inside the Asset::img(..)
How to do this? what to put in place of $work->cover_img?
Edit: I was able to get the result by using this:
<img src="<?php echo Uri::base(false); ?>/assets/img/project-icons/icon/<?php echo $work->cover_url; ?>" class="img-responsive" alt="..." />
Can this be achieved by using Asset::img() instead of Uri::base()?
Can't you just do:
<?php
echo Asset::img('project-icons/icon/'.$work->cover_img, array('class'=>'img-responsive', 'alt'=>'...'));
?>
...?

Image doesnt show up in php

In this little snippet of code ,i show how i take the "foto1" column of my database and transfer the value of it to a variable in c# named $foto.
The $foto contains the path of the image corresponding to the product that is showing up. Ive tried to copy and paste the path and ditch out the php part and it works. But when i put it in img src it gives me like the broken image thing.And i cant figure out why it does that.
All help is aprecciated .
Have a nice day :)
<div class="row shop_box-top">
<?php
$ligaBD=odbc_connect('basededadospap','','');
$sql="SELECT * FROM produto WHERE nome_produto LIKE '%ADIDAS%'";
$resultado=odbc_exec($ligaBD,$sql);
?>
<div class="row shop_box-top">
<?php
while (odbc_fetch_row($resultado))
{
$nome = odbc_result($resultado,2);
$preco= odbc_result($resultado,4);
$foto = odbc_result($resultado,9);
?>
<div class="col-md-3 shop_box"><a href="stansmithflatwhite.html">
<img src="<?php echo $foto; ?>" class="img-responsive" alt=""/>
<span class="new-box">
<span class="new-label">Novo</span>
</span>
<div class="shop_desc">
<h3><?php echo $nome; ?></h3>
<span class="actual"><?php echo $preco; ?></span><br>
</div>
</a></div>
<?php }?>
depends of what path contains the $foto var. If is the absolute path, you have to retrive the relative path.
Try also to append an / or an http[s] in front of the path
<img src="/<?php echo $foto;?>">
So it would be : //path/to/photo
As I can see it in your comment, your image paths contain spaces, so a possible solution can be to use urlencode() before echoing them.
Try passing full path to img tag like http://localhost/xyz/images/Cal�ado/Adidas/Homem/Stan Smith/ADIDAS STAN SMITH - RED/ch-adidas-stan-smith-red-5.jpg.
Replace "localhost/xyz" with your website directory path.

bootstrap image row out of place

[SOLVED]
I'm have written a horizontal row of 4 images 500x300 using placeholders, this works perfectly fine, but when I try to replace those images with images from a folder (using php) of the same dimensions, the first 2 image containes are knocked out of place while the last 2 are where they belong.. I've attached a screenshot and the code below is what I'm using to get the row of images.
<div class="row" style="margin-top:1px">
<?php
$directory = 'images';
if (! is_dir($directory)) {
exit('Invalid diretory path');
}
$files = array();
foreach (scandir($directory) as $file) {
?>
<div class="col-sm-3 col-xs-6" >
<a href="#" >
<img class="img-responsive portfolio-item" src="<?php echo "images/$file"; ?>" alt="">
</a>
</div>
<?php
}
?>
</div>
this is what i get in the browser
Using glob instead of an array fixed the problem. PHP is including the . and double dot of the directory structure and therefore creating 2 empty images.

Create new line for each seperated element in field (database)

Here's my code :
<div id="slides" class="col-md-7" >
<img src="'.$row['img2'].'" >
</div>
In that "img2" row in the database, it's image path. But I need to detect if there's more than one image path seperated by a comma in there.
So if there's 2 images in the field (ex.: img/image1.jpg, img/image2.jpg), well the HTML would look like :
<div id="slides" class="col-md-7" >
<img src="'.$row['img2(first value)'].'" >
<img src="'.$row['img2(second value)'].'" >
</div>
But if there's only 1 value, it stays like my first code part.
Anyone? Thanks a lot
use the explode function and then a foreach loop
<div id="slides" class="col-md-7" >
<?php
$images = explode(',', $row['img2']);
foreach($images as $image) {
?>
<img src="<?php echo $image; ?>" >
<?php } ?>
</div>

Categories