Jump to:

23001 Posts in 11702 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » Get image width using url of the image

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

Page: 1
Go to End
Author Topic: 315 Views
  • Sultan
    Avatar
    Community Member
    8 Posts

    Get image width using url of the image Link to this post

    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...

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: Get image width using url of the image Link to this post

    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....

  • Sultan
    Avatar
    Community Member
    8 Posts

    Re: Get image width using url of the image Link to this post

    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.

    315 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.