note 60629 added to function.getimagesize

From: Date: Thu, 12 Jan 2006 18:38:31 +0000
Subject: note 60629 added to function.getimagesize
Groups: php.notes 
Request: Send a blank email to php-notes+get-101644@lists.php.net to get a copy of this message
Although this code does not utilize getImageSize - it is directly related. In trying to create PDF pages with images on it, I developed a function designed to generate the image size required to fit to a specific page size. It turns out that since the page is rectangular, in some cases if you resize to fit by any one (and only one) of the dimensions, the other dimension may still be past the page size. I did some math and came up with something that seems to fit and keep the image maximized within the page. I ran the KEEP_ON_PAGE_BOTH algorithm through a double nested for loop testing all image height and width combinations between 1 and 10,000, checking for overages or images for which both dimensions were more than 12 pixels away from the edge of the page (eg one dimension had to at least be near the edge of the page) - it seemed to have passed the test. The beastly code below gives several options: FORCE_FIT_BOTH - returns dimensions the same as the page size. Image will fit, but may be distorted. FORCE_FIT_WIDTH - returns dimensions as wide as the page and with appropriate aspect ratio based size for height. Height could possibly be larger than the page, depending on image size. FORCE_FIT_HEIGHT - returns dimensions as high as the page and with appropriate aspect ratio based size for width. Width could possibly be larger than the page, depending on image size. KEEP_ON_PAGE_BOTH - returns dimensions less than or equal to the page dimensions, using the algorithm described above. If image is smaller than the page in both dimensions, the dimensions returned will be the original size. Note: another possible version (not written) could scale the image purposefully larger than the page and then use the algorithm to assure fit- which would essentially be a scaled fit to page function that forces a smaller image to be enlarged to page size (as opposed to keeping it original size). KEEP_ON_PAGE_WIDTH - returns dimensions less than or equal to the page dimensions, not to exceed the page width, but maintains aspect ratio for the height. It is possible the width could exceed the page dimensions, depending on the image. KEEP_ON_PAGE_HEIGHT - returns dimensions less than or equal to the page dimensions, not to exceed the page height, but maintains aspect ratio for the height. It is possible the height could exceed the page dimensions, depending on the image. The function was too long to post so here's a link: http://www.webtrotter.com/phpGetImageSize.txt ---- Manual Page -- http://www.php.net/manual/en/function.getimagesize.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+60629 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60629&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60629&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60629&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60629&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60629&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60629&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60629&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+60629&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#101644) next »