Custom Image Dimensions in wordpress 4.1
-
Hi There, I am hoping that someone can help me with a new issue that I am experiencing in WordPress. I have been using custom images for quite a while on my website.
These get created in my functions.php file, and all images gets resized from 1920px to 150px, 240px, 300px, 468px, 540px, 677px, 720px, 750 and 1920px. These are then served to mobile devices, tablets or desktop’s depending on the resolutions needed.
The problem that I have is that previously the script would pick the correct (Height) of each image as wordpress saved it into the db and on the disk itself.
- image-150×150.jpg
- image-240×159.jpg
- image-300×199.jpg
- image-468×311.jpg
- image-540×359.jpg
- image-677×450.jpg
- image-720×479.jpg
- image-750×499.jpg
The original file that I upload is always 1920px width irrespective of the height of the image. In the above case the image width is 1920px and the height is 1279px
To calculate the dimensions for the smaller images I use the following formula:
- original height / original width x new width = new height
This will give me the height of all smaller images
- 240×159.875px and wordpress would save it as 159px height
- 300×199.8438px and wordpress would save it as 199px height
- 468×311.7563px and wordpress would save it as 311px height
- 540×359.7188px and wordpress would save it as 359px height
- 677×450.9807px and wordpress would save it as 450px height
- 720×479.625px and wordpress would save it as 479px height
- 750×499.6094px and wordpress would save it as 499px height
I am not sure how wordpress handled the image dimensions previously, but now in the script where I am using the formula original height / original width x new width = new height the values are not the same as what wordpress would save in the db and on the disk.
eg an image with dimensions 1920px x 1518px will create the following files on the hard disk
- img-240×190.jpg and the height not rounded 189.75px
- img-300×237.jpg and the height not rounded 237.1875px
- img-468×370.jpg and the height not rounded 370.0125px
- img-540×427.jpg and the height not rounded 426.9375px
- img-677×535.jpg and the height not rounded 535.253125px
- img-720×569.jpg and the height not rounded 569.25px
- img-750×593.jpg and the height not rounded 592.96875px
Now if I place a round() php code inside my script I am able to get eg the image file like “img-750×593.jpg” on the front end to look for the image 750×593.jpg and find it.
However 100’s of previously uploaded images seems to break when I use round() I suspect that in WordPress 4.1 the image rounding is working differently than in previous versions, and I cannot figure out how to accommodate the new (image upload round()) function to be backwards compatible with my previously uploaded images, without breaking them…
If I am correct, in previous wordpress versions the images were rounded up or down, differently than to how the latest WP4.1 is rounding up or down images.
From what I am guessing in previous word press versions if a file had the size of 1920px x 1518px wordpress would have saved the height for a scaled image of 750px at 750px x 592px (592.96875px) without rounding up the image to 750px x 593px and now it looks like wordpress is saving the file at 750px x 593px and not as 750px at 592px, which results in a if I fix the roundup to accommodate the new upload images roundup feature then all the old images breaks, and if I fix it for the old one’s new uploaded images might not display correctly if they need to be round up…
- The topic ‘Custom Image Dimensions in wordpress 4.1’ is closed to new replies.