Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Get image width using url of the image


Reply


3 Posts   501 Views

Avatar
Sultan

Community Member, 8 Posts

21 September 2013 at 11:40pm

Edited: 21/09/2013 11:41pm

I am trying to get the image width from the url of the image. Is that possible ??
What i am trying to do is, if a user uploads image from the editor and if the width of image uploaded is greater than 700px then i will resize the image and use that in place of the original one. I am grabbing all the images in the editor like this

$doc = new DOMDocument();
$doc->loadHTML($this->PageAdvancedPopup_html);
$imageTags = $doc->getElementsByTagName('img');
   foreach($imageTags as $tag) {
$imgs = $tag->getAttribute('src');
    echo "<img src='/$imgs' />";
}

Now how can i check the width of these image ??
Please help me I am stuck...

Avatar
Bambii7

Community Member, 254 Posts

23 September 2013 at 6:36pm

Hi Sultan,
You wont be able to do it via the URL. You'll need to actually load the image first to read that property. Unless of course you control the HTML you could output it in the <img> tag.

To do this yourself first load the image with imagecreatefromjpeg http://php.net/manual/en/function.imagecreatefromjpeg.php
then read width with getimagesize http://www.php.net/manual/en/function.getimagesize.php
It'll be the first item in the array of getimagesize.

Eg.

$image = imagecreatefromjpeg( 'http://mysite.com/example.jpg' );
$dimensions = null;
getimagesize( $image, $dimensions );
echo 'width = ' . $dimensions[0];

I haven't tested the code, you may need to write the file first....

Avatar
Sultan

Community Member, 8 Posts

23 September 2013 at 11:40pm

Thanks Bambii7 for your reply. I tried the examples in the link provided by you. But width and height is coming blank.

Can u please share a working example.