
Get image sizes with 100% pure Miva Script
Written by Claudiu Bischoff, on 12 mai 2010
OVERVIEW
In websites, we have to deal with lot of images. Sometimes, we need to know the width and the height of the images without any extra help (php or javascript). This operation wasn't available in Miva Script so here is a tiny library that will do a simple thing. It returns you the width and the height of the image. It works (for the moment) only with JPEG, GIF and PNG. Those are the most used image formats on the web. Other images will be added in the future.
Calling the function is simple:
<mvdo file="/path_to_th_lib/getimagesize.mvc"
name="l.img"
value="{getimagesize('/path_to_your_image/image.png',
'DATA')}">
You can call the image from the 'DATA' or the 'SCRIPT' side of your server. You will get in return a structure. In our example, the return will be the folowing:
l.img:width = the width of the image in pixels
l.img:height = the height of the image in pixels
l.img:error = some error, if something is wrong
l.img:kb = weight of the file in kilo-bytes.
l.img:version = version & copyright of the library.
Tip: Check first the l.img:error to see if the lib throws you any error, else you could get unexpected results ( width and height will be emty strings). If no error, the l.img:error will get bak as an empty string.
If you are really curious, the script is only reading the header of the images and grab infos directly from the binary data.
COMPATIBILITY
You must use Miva Empresa 4+ (some functions used in the library aren't available in lower versions of Empresa)
THE CODE
How to call the function :
<mvdo file="/path_to_th_lib/getimagesize.mvc" name="l.img" value="{getimagesize('/path_to_your_image/image.png', 'DATA')}">
DOWNLOAD (attention, BETA code)
By downloading this script, you agree with the terms of licence.
Direct download of the library (right click and save as...):
click here
BE AWARE : It's a BETA library. The script wasn't tested a lot, so if I were you, I wouldn't use it in a production environement without testing it more.
TODO
I already work on more image formats and grabbing other image informations (like date of the image, etc...)
LICENCE
GPL & MIT - You can use it as you want. If you like my work, put wherever you want a link to mivascript.org website. I really hope this way, more people will get to know better "Miva Scripting". The code source will be available only to the members of the mivascript.org website (it will be available sometimes soon). By downloading the library, you agree with the terms below:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
COMMENTS
I started a thread in the Miva Merchant (Miva Script section), so don't hesitate: http://extranet.mivamerchant.com/forums/showthread.php?t=100238

