checking image size when converting BBCode text - php

on my mission to use ckEditor as a BBCode editor i am faced with an issue directly for my site.
I have user submitted content that can contain [img] tags.
I really want the images to have a width of 100%, which is fine. I convert any [img] tags to html and apply a class which is styled as i wish.
function basicbbcode($text) {
$text = str_replace("[IMG]", "<img class='buildimage' src='", "$text");
$text = str_replace("[/IMG]", "'>", "$text");
$text = str_replace("[img]", "<img class='buildimage' src='", "$text");
$text = str_replace("[/img]", "'>", "$text");
}
The issue is when someone uses a small / low res image. I dont wish to stretch that image because its going to look horrible.
My aim was to find a threshold, so if the image is more than 800px wide, give it the class of buildimage. If its smaller, give it a class of buildimage-small.
I would then just keep the natural size of that image. I am trying to promote the users providing high quality images but at the same time want to keep the site looking great and no having poorly stretched images because there is res is small.
So, is there a way to check the image size when it is out from the database as the $text variable and then act accordingly. Ideally with php, but maybe jquery?
No idea on this one, possible?

You can use getimagesize():
The getimagesize() function will determine the size of any given image
file and return the dimensions along with the file type and a
height/width text string to be used inside a normal HTML IMG tag and
the correspondant HTTP content type.
<?php
list($width, $height, $type, $attr) = getimagesize("img/flag.jpg");
?>
Source: http://us3.php.net/getimagesize

To do it in jQuery it could be as simple as:
var box = $("#boxWithImageInside"),
img = box.find("img.buildimage");
if (img.width() > box.width()) {
img.width("100%");
img.height("auto");
}
The code was not tested, so you may have to adjust it to to what you need.

If you want to solve your problem on client side maybe this helps : FIDDLE
$(function () {
isImgLargerThan800px = function (src) {
var img = new Image();
img.onload = function () {
if (this.width < 800) {
alert('this image need SMALL class')
} else {
alert('this image need LARGE class')
}
};
img.src = src;
};
//TEST
$('a').on('click', function () {
isImgLargerThan800px($(this).text());
alert('Now checking for image size of URL : ' + $(this).text())
})
});

Related

Cut corners in image PHP

Im working with Intervention Image to make a frame. Now im stuck on a part to cut off the edges. I need 4 of those images to make them fit as a frame. I know i can use the Intervention Image library to rotate the image, but i have no clue to cut those corners. Anyone has a idea how to achieve this?
Original:
Result:
You need to create two polygons and fill them with transparent colors.
http://image.intervention.io/api/polygon
An example:
$img = Image::make('foo/bar/baz.jpg')->encode('png');
$w = $img->width();
$h = $img->height();
$points = [0,0,$width,0,$width,$width,0,0];
$img->polygon($points, function($d) {
$d->background("transparent");
});
$points = [0,$height,$width,$height,$width,$height-$width,0,$height];
$img->polygon($points, function($d) {
$d->background("transparent");
});
$img->save('foo/bar/baz_cut.png'); // jpg won't have transparency

Add image dimensions automatically to <img>

I scored my website on gtmetrix.com.
It recommends adding image dimensions to all my images. The thing is, I need some way of adding these automatically, so if the image changes, the dimentions are updated.
What I have
<img src="bla.png">
What I'd like to have
<img src="bla.png" width="{width of image}" height="{height of img}">
Is there a php way or a jquery way to do this?
For a wordpress site.
PHP:
<?php
list($width, $height) = getimagesize("bla.png");
echo "<img src='bla.png' width='$width' height='$height'>";
?>
You can use image onload event to calculate the dimensions of image. Pure javascript approach, something like ;
var image = new Image();
image.onload = function()
{
//after load complete you will get the image dimensions here from
//image.width and image.height
}
image.src = 'image url';
In php it's getimagesize($file) if you are outputting images dynamically.
You should do it with PHP not with javascript. If you add it with jQuery / javascript, the width and height would be added after loading. The main reason of specifying width and height is to let the engine render the page faster. It would not be of any help if it as added after loading.
You can use the index 3 of the returned array from getimagesize() directly in the image tag. It contains a string with height="yyy" width="xxx" an can be used directly in an IMG tag.
<?php
list($width, $height, $type, $attr) = getimagesize("file.png");
echo "<img src='file.png' $attr />";
?>
You can visit http://php.net/manual/en/function.getimagesize.php for more details on this function.

Add or Remove HTML code on Windows Resize

I'm working on a responsive website. I'm using CSS Media Queries and Adaptive Images (http://adaptive-images.com/) to render each page correctly on different devices.
My problem is that some elements are downloaded by the browser even if there are hidden! For instance, a element with the property {display:none;} is downloaded despite the fact that I don't want it.
So, here is my question: is there a way to add or remove HTML code depending on the device's screen-size?
By reading the code of "Adaptive Images" script, I saw they used a cookie, which stores the screen-size value.
<script>
document.cookie='resolution='+(window.innerWidth)+'; path=/';
window.addEventListener("resize", function() {
document.cookie='resolution='+(window.innerWidth)+'; path=/';
}, false);
</script>
So, I tried to read this cookie with a PHP function:
<?php
$fallback_res = 481;
$res = !empty($_COOKIE['resolution']) ? $_COOKIE['resolution'] :
$fallback_resolution; // Get the viewport resolution in $res variable
?>
It allows me to write a very useful PHP condition :
<?php if(isset($res) AND $res >= $fallback_res) // if viewport >= 481px
{
echo '<video>...</video>'; // Display the video
}
else { //if the screen is smaller
echo ''; // Echo something else
}
?>
It work very well. If the viewport is < 481px, the video is not downloaded. If the viewport is > 481px, the video is downloaded. But it only works, on page load! If I load the page with a 480-wide-opened-browser (=video not loaded), and then resize my browser to higher resolution, there is a big hole in the middle of the page.
What I need is to reload some part of the code on window resize. (Each time I resize the browser window, I want the $res condition to be updated automatically, and the following code as well)
Thanks.
if you combine blow jquery code with ajax , maybe you can do that .
$(window).resize(function() {
if($(window).width() > 480 )
{
#ajax code for download video
#or use .after in jquery
}
})
Html will looks like
<div id="phone">Content For the below 450px </div>
<div id="desktop">Content For the above 450px </div>
Add jQuery
if($(window).width() < 450 )
{
$('#desktop').remove();
}
else
{
$('#phone').remove();
}

Get image height that is not in content

I am building a custom wordpress theme and have a slideshow on the front page displaying images from blog posts.
Now if the image is over 300px in height I want to force its widthand center it vertically. So my question is:
Is there a way, in php or javascript to get the height of an image by only using the URL of the image?
EDIT: I tried this: <?php list($height) = getimagesize(echo catch_that_image()); echo "<p>$height</p>"; ?> where catch_that_image() returns the URL, but somehow this doesn't work (I think the first echo breaks it). Any ideas?
Thank you for your help.
image by only using the URL of the image?
var img = new Image();
img.onload = function() {
alert('The size is ' + this.width + 'x' + this.height);
};
img.src = 'some image URL';
Try it
http://jsfiddle.net/NPSMN/
ps:
I tried this: ... where
catch_that_image() returns the URL, but somehow this doesn't work (I
think the first echo breaks it). Any ideas?
did you look closer at your code? echo outputs to the browser, not to the argument of the function.
<?php
list(,$height) = getimagesize(catch_that_image());
echo "<p>$height</p>";
?>
See getimagesize() in PHP. That will do what you're looking for.
http://php.net/manual/en/function.getimagesize.php
<?php
$url = "http://www.example.com/image.jpg";
list($width, $height) = getimagesize($url);
echo "Width: $width<br />Height: $height";
?>

Setting a hard limit for image size using PHP

I have this script for displaying a slideshow for a client.
I'm looking for a quick way of making the "img" not go over a certain size.
This is my original code:
<script type="text/javascript">
$(function() {
$('#test2').crossSlide({
speed: 15,
fade: 1
}, [
<?php
$directory = "photos/";
$images = glob("" . $directory . "*.jpg");
$arrLength = count($images);
foreach($images as $key=>$image){
echo("{src: '$image', dir: '$quotes[$random]'}");
if($key < $arrLength - 1){ echo ", "; }
}
?>
]);
});
</script>
Prehaps I could use some sort of external file?
Link it like.. image.php?img=foo.jpg (with the max script in there?)
Any help would be great,
Thanks!
getimagesize() is a start.
if( array_product(getimagesize($image)) < 2500 ) { // Check area
// do stuff
}
list($width, $height) = getimagesize($image);
if( $width < 500 && $height < 400 ) { // Check dimensions
// do stuff
}
In the future, do a quick Google. PHP is a language that supports a lot out of the box. Often times, you'll be able to find something in the docs.
Provided that images are uploaded to the website, you should not allow images to be uploaded that are over a certain size of megabytes.
If you're talking about dimensions, you can use the max-width: and max-height CSS attributes to make sure images don't go over a certain size.
If you're looking to scale images (or anything else) proportionately, to fit the size of their container, you can use the JQuery image resize plugin for that.

Categories