controlling the size of an image when clicked - php

The following php script is used to load an image from the database! when the image is clicked an enlarged image appears but when i give height and width it remains the same large size! I want to reduce the size of the clicked image to
height=100 and width=100 Though i gave them it did not work
echo'<div class="ienlarger"><a href=../ordering/'.( $row['Image1'] ).' height=100 width=100><img src="../ordering/' .$row['Image1']. '" alt="thumb" class="resize_thumb" /><span>
<img src="../ordering/' .$row['Image1']. '" alt="large" height=500 width=500 /><br />
Copy-1</span></a></div>';
How can achieve this?

The reason why the clicked image isn't 100x100 is because there is no width or height attributes in the a tag. If you think about it, since a tag or anchors can link to all types of documents, including non-image files and non-html files (i.e. it could link to a file download). And in the case of a download, it wouldn't make sense to have a width attribute.
I would suggest if you really want this functionality, you could either create an image 500x500 resolution and create a similar one with a 100x100 resolution....
Or if you can't afford that space, you could create a javascript that does that. For example (not sure if it works):
<div class="ienlarger"><img src="../ordering/<?php echo $row['Image1'] ?>" alt="thumb" class="resize_thumb" />
<span>
<img id="sizable" src="../ordering/<?php echo $row['Image1'] ?>" alt="large" height=500 width=500 /><br />
Copy-1
</span>
<script>
document.getElementById("sizeable").onclick = function(evt)
{
evt.target.width = 100;
evt.target.height = 100;
};
</script>
</div>

Related

How do I populate Image gallery using PHP on html page, including image code/classes either side?

I am creating a webpage that has a photo gallery, which is a lightbox, that uses a lot of images - around 80-150 per page. The number of the images changes week by week, and I would like to have the website automatically populate the image gallery from the images in a subfolder, whilst including the code attached to the image to make it display correctly.
For example, this is what each images code will look like. And please note that i'll need the image located twice on each line.
<a data-fancybox="gallery" href="images/001.jpg"><img alt="" class="lazy" data-src="images/001.jpg" /></a>
I am attempting to use the below script, but it doesn't appear to be working.
<?php
$dirname = "../images/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<a data-fancybox="gallery" href="'.$image.'"><img alt=""
class="lazy" data-src="'.$image.'" /></a>';
}
?>
In this case for each line I have included the .$image. in two locations, inbetween the echo's but it doesn't seem to be working.
If you have any advice for me it will be greatly appreciated.
Your <img /> tag doesn't have the path set in the src attribute which is needed to render the image by the browser. It only has data-src attribute filled.
Should be:
<?php
$dirname = "../images/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<a data-fancybox="gallery" href="'.$image.'"><img alt=""
class="lazy" src="'.$image.'" data-src="' . $image . '" /></a>';
}
?>

How to fix image width and height for a string can contain multiple images

I have a multi templates of contents (strings) stored in a database, some of these strings have images with a defined height and width, some without these attrebutes, and some with invalid attributes values.
I need to pass all these strings over a function that can fix this issue, and set a real width and height attributes based on the real image size, it's easy, i have trubbles doing this and save the image location inside the string without changing it.
example:
before fix:
some text <img src="/images/image.jpg" alt"some alt" /> another some text <img src="/images/image2.jpg" alt="another some alt" width="519" /> agian some text <img src="/images/i.jpg" width="519" height="450" alt="text alt" /> <p> <img src="/images/image2.jpg" alt="another some alt" height="400" /></p>
after fix, all images have width and height
some text <img src="/images/image.jpg" width="530" height"600" alt"some alt" /> another some text <img src="/images/image2.jpg" alt="another some alt" width="519" height"400" /> agian some text <img src="/images/i.jpg" width="519" height="450" alt="text alt" /> <p> <img src="/images/image2.jpg" alt="another some alt" width="800" height="400" /></p>
Thanks
Are all the images meant to be the same size? If so then use a CSS class to set their dimensions as this will override the width and height attributes.

viewing an image recieved from database in form of an blob datatype to a new page in php

I have made an code which is used to receive image from database and it's working perfectly. But now as user click's on the image it should be opened on a new page,and i have tried few thing but it's not working...
the below code id used to display the image
$query=mysqli_query($conn,"select * from images") or die("unable to
connect");
$i=1;
while($row=mysqli_fetch_array($query,MYSQLI_BOTH))
{
//echo '<img id="my" height="150" width="320" src="data:image;base64,'.$row['image'].' "> ';
echo '
<tr>
<t>'.#$row["id"].'</td>
<a target="_blank" href="09.jpg">
<img height="150" width="150" src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'"/>
<td>x</td>
</td>
';
$i++;
}
?>
Now here i want is when user click's on the image , a new page will show an enlarged image...
Don’t save image in database, save it as file in server then save the file name into database
You must be save image as two sizes, small and normal, the smal for display in table, when the user click on image, go to normal image
You can save one name in database, example (img1.jpg)
And in images directory save (sm-img1.jpg) and (img1.jpg)
The code
<a href="url/mydirectory/$row['image']" target="_blank">
<img width="150" height="150" src="url/mydirectory/sm-$row['image']" />
</a>
Or
<img width="150" height="150" src="url/mydirectory/sm-$row['image']" onclick="window.open('url/mydirectory/$row['image']', '_blank');" />

PHP + jQuery gallery

I have this code:
<img src="'. $random_pics[$i] .'" width="'.$thumb_width.'" alt="" name="?album='. urlencode($albums[$i]) .'" class="alb" />
<div id="pictures"></div>
<script>
$('.alb').click(function(){
var href = $(this).attr('name');
$("#pictures").load('display.php'+href)
});
</script>
Then I have a display.php file, where I use $_GET['album'] to get the album name and display the photos from that album.
How it should work: when I click the image, the pictures div should load the display.php?album=somealbum, but for some reason it does not... My files are bigger, but I think the problem is here.
It looks like you missed the php tags around each of those embedded php codes. Or perhaps there should be more to the snippet you provided?
<img src="<?php echo $random_pics[$i]; ?>"
width="<?php echo $thumb_width; ?>"
alt=""
name="?album=<?php echo urlencode($albums[$i]); ?>"
class="alb" />

how to insert this javascript in php

how to insert this javascript code :
thejavascript
inside img src in php
<img src="http:'.$iv[$j].'.jpg" height="220" width="200" alt="Image '.ucwords($kw).'" "thejavascript">
Thanks
You can use this jQuery script to add and onClick event in your balise img but you must adding an attribute id into your image. An id must be unique in your html page.
$("img#idImage").click(function({
window.open('welcome.html','welcome');
}));
$kw = $ucwords($kw);
"<img id='idImage' src='http:{$iv[$j]}.jpg' height='220' width='200' alt='Image {$kw}' />";
But the best will be to separate attributes height and width into a CSS stylesheet.
Insert the following HTML outside PHP brackets and it should work according to the way you posted it. I'm making a few assumptions, one being that the link you posted wraps the image code and that the PHP variables turn the image into valid code.
<a href="javascript:void(0);" onclick="window.open('welcome.html','welcome')">
<img src="http:<?=$iv[$j]; ?>.jpg" height="220" width="200" alt="Image <?=ucwords($kw); ?>">
</a>
More simple, replace "thejavascript" (with the "") by :
onclick="window.open(\'welcome.html\',\'welcome\')"
A bit different approach, onclick event is on img tag.
$javascript="window.open('welcome.html','welcome')";
$img='<img src="http:'.$iv[$j].'.jpg" height="220" width="200" onclick="'.$javascript.'">';
echo $img;

Categories