📜 ⬆️ ⬇️

Features of using GD-lib on PHP hosting with limited resources

This problem had to be dealt with after the event on the hosting “X”, where rather small images could not be converted using this library. But the worst thing was that the script just died, leaving the information only in the logs. So I had to find out

From reliable sources (logs) it was known that there is not enough memory to copy the image into it. Those. it was necessary to preliminarily check its sufficient availability before using the library (the variant manipulation with memory_limit is not considered), having foreseen how much memory it is going to grab.

So, the GD library can create images with a palette of 2 ^ 8 colors and 2 ^ 24. T.zh. theoretically, the maximum image size is 2 ^ 16 x 2 ^ 16. Total per pixel is 5 or 7 bytes.
')
Let's test it in practice - create square images with the createImage and createImageTrueColor functions. And let us reduce the practice and theory in one graph:



It can be seen that the results diverge, but the theory for 8 bits and practice for 24 bits practically coincide.

Let's try to get the empirical values ​​of "average memory cost" by 1 pixel in two palettes. We build graphics.



It is seen that for large values ​​they asymptotically tend to 2 and 5 bytes, respectively.

But with small image sizes (approximately 100 pixels per side) they can reach 3 and 6 bytes per pixel. We will not bother with the approximating function (although it would be worth it) and just take an average of 2.5 and 5.5 bytes per pixel, respectively.

As a result, we obtain the following function to test the possibility of using the GD library in the current operational environment:

Copy Source | Copy HTML function returnBytes( $v ) { $v = trim( $v ); switch (strtolower( $v [strlen( $v )- 1 ])) { case 'g' : $v *= 1024 ; case 'm' : $v *= 1024 ; case 'k' : $v *= 1024 ; } return $v ; } function checkMemoryForGDUsage( $w , $h , $trueColor = false ){ return (((returnBytes(ini_get( "memory_limit" ))-memory_get_usage())) > ( $w * $h * ( 2 . 5 + (((int) $trueColor ) * 3 ))))? true : false ; }
  1. Copy Source | Copy HTML function returnBytes( $v ) { $v = trim( $v ); switch (strtolower( $v [strlen( $v )- 1 ])) { case 'g' : $v *= 1024 ; case 'm' : $v *= 1024 ; case 'k' : $v *= 1024 ; } return $v ; } function checkMemoryForGDUsage( $w , $h , $trueColor = false ){ return (((returnBytes(ini_get( "memory_limit" ))-memory_get_usage())) > ( $w * $h * ( 2 . 5 + (((int) $trueColor ) * 3 ))))? true : false ; }
  2. Copy Source | Copy HTML function returnBytes( $v ) { $v = trim( $v ); switch (strtolower( $v [strlen( $v )- 1 ])) { case 'g' : $v *= 1024 ; case 'm' : $v *= 1024 ; case 'k' : $v *= 1024 ; } return $v ; } function checkMemoryForGDUsage( $w , $h , $trueColor = false ){ return (((returnBytes(ini_get( "memory_limit" ))-memory_get_usage())) > ( $w * $h * ( 2 . 5 + (((int) $trueColor ) * 3 ))))? true : false ; }
  3. Copy Source | Copy HTML function returnBytes( $v ) { $v = trim( $v ); switch (strtolower( $v [strlen( $v )- 1 ])) { case 'g' : $v *= 1024 ; case 'm' : $v *= 1024 ; case 'k' : $v *= 1024 ; } return $v ; } function checkMemoryForGDUsage( $w , $h , $trueColor = false ){ return (((returnBytes(ini_get( "memory_limit" ))-memory_get_usage())) > ( $w * $h * ( 2 . 5 + (((int) $trueColor ) * 3 ))))? true : false ; }
  4. Copy Source | Copy HTML function returnBytes( $v ) { $v = trim( $v ); switch (strtolower( $v [strlen( $v )- 1 ])) { case 'g' : $v *= 1024 ; case 'm' : $v *= 1024 ; case 'k' : $v *= 1024 ; } return $v ; } function checkMemoryForGDUsage( $w , $h , $trueColor = false ){ return (((returnBytes(ini_get( "memory_limit" ))-memory_get_usage())) > ( $w * $h * ( 2 . 5 + (((int) $trueColor ) * 3 ))))? true : false ; }
  5. Copy Source | Copy HTML function returnBytes( $v ) { $v = trim( $v ); switch (strtolower( $v [strlen( $v )- 1 ])) { case 'g' : $v *= 1024 ; case 'm' : $v *= 1024 ; case 'k' : $v *= 1024 ; } return $v ; } function checkMemoryForGDUsage( $w , $h , $trueColor = false ){ return (((returnBytes(ini_get( "memory_limit" ))-memory_get_usage())) > ( $w * $h * ( 2 . 5 + (((int) $trueColor ) * 3 ))))? true : false ; }
  6. Copy Source | Copy HTML function returnBytes( $v ) { $v = trim( $v ); switch (strtolower( $v [strlen( $v )- 1 ])) { case 'g' : $v *= 1024 ; case 'm' : $v *= 1024 ; case 'k' : $v *= 1024 ; } return $v ; } function checkMemoryForGDUsage( $w , $h , $trueColor = false ){ return (((returnBytes(ini_get( "memory_limit" ))-memory_get_usage())) > ( $w * $h * ( 2 . 5 + (((int) $trueColor ) * 3 ))))? true : false ; }
  7. Copy Source | Copy HTML function returnBytes( $v ) { $v = trim( $v ); switch (strtolower( $v [strlen( $v )- 1 ])) { case 'g' : $v *= 1024 ; case 'm' : $v *= 1024 ; case 'k' : $v *= 1024 ; } return $v ; } function checkMemoryForGDUsage( $w , $h , $trueColor = false ){ return (((returnBytes(ini_get( "memory_limit" ))-memory_get_usage())) > ( $w * $h * ( 2 . 5 + (((int) $trueColor ) * 3 ))))? true : false ; }
  8. Copy Source | Copy HTML function returnBytes( $v ) { $v = trim( $v ); switch (strtolower( $v [strlen( $v )- 1 ])) { case 'g' : $v *= 1024 ; case 'm' : $v *= 1024 ; case 'k' : $v *= 1024 ; } return $v ; } function checkMemoryForGDUsage( $w , $h , $trueColor = false ){ return (((returnBytes(ini_get( "memory_limit" ))-memory_get_usage())) > ( $w * $h * ( 2 . 5 + (((int) $trueColor ) * 3 ))))? true : false ; }
  9. Copy Source | Copy HTML function returnBytes( $v ) { $v = trim( $v ); switch (strtolower( $v [strlen( $v )- 1 ])) { case 'g' : $v *= 1024 ; case 'm' : $v *= 1024 ; case 'k' : $v *= 1024 ; } return $v ; } function checkMemoryForGDUsage( $w , $h , $trueColor = false ){ return (((returnBytes(ini_get( "memory_limit" ))-memory_get_usage())) > ( $w * $h * ( 2 . 5 + (((int) $trueColor ) * 3 ))))? true : false ; }
  10. Copy Source | Copy HTML function returnBytes( $v ) { $v = trim( $v ); switch (strtolower( $v [strlen( $v )- 1 ])) { case 'g' : $v *= 1024 ; case 'm' : $v *= 1024 ; case 'k' : $v *= 1024 ; } return $v ; } function checkMemoryForGDUsage( $w , $h , $trueColor = false ){ return (((returnBytes(ini_get( "memory_limit" ))-memory_get_usage())) > ( $w * $h * ( 2 . 5 + (((int) $trueColor ) * 3 ))))? true : false ; }
  11. Copy Source | Copy HTML function returnBytes( $v ) { $v = trim( $v ); switch (strtolower( $v [strlen( $v )- 1 ])) { case 'g' : $v *= 1024 ; case 'm' : $v *= 1024 ; case 'k' : $v *= 1024 ; } return $v ; } function checkMemoryForGDUsage( $w , $h , $trueColor = false ){ return (((returnBytes(ini_get( "memory_limit" ))-memory_get_usage())) > ( $w * $h * ( 2 . 5 + (((int) $trueColor ) * 3 ))))? true : false ; }
  12. Copy Source | Copy HTML function returnBytes( $v ) { $v = trim( $v ); switch (strtolower( $v [strlen( $v )- 1 ])) { case 'g' : $v *= 1024 ; case 'm' : $v *= 1024 ; case 'k' : $v *= 1024 ; } return $v ; } function checkMemoryForGDUsage( $w , $h , $trueColor = false ){ return (((returnBytes(ini_get( "memory_limit" ))-memory_get_usage())) > ( $w * $h * ( 2 . 5 + (((int) $trueColor ) * 3 ))))? true : false ; }
  13. Copy Source | Copy HTML function returnBytes( $v ) { $v = trim( $v ); switch (strtolower( $v [strlen( $v )- 1 ])) { case 'g' : $v *= 1024 ; case 'm' : $v *= 1024 ; case 'k' : $v *= 1024 ; } return $v ; } function checkMemoryForGDUsage( $w , $h , $trueColor = false ){ return (((returnBytes(ini_get( "memory_limit" ))-memory_get_usage())) > ( $w * $h * ( 2 . 5 + (((int) $trueColor ) * 3 ))))? true : false ; }
  14. Copy Source | Copy HTML function returnBytes( $v ) { $v = trim( $v ); switch (strtolower( $v [strlen( $v )- 1 ])) { case 'g' : $v *= 1024 ; case 'm' : $v *= 1024 ; case 'k' : $v *= 1024 ; } return $v ; } function checkMemoryForGDUsage( $w , $h , $trueColor = false ){ return (((returnBytes(ini_get( "memory_limit" ))-memory_get_usage())) > ( $w * $h * ( 2 . 5 + (((int) $trueColor ) * 3 ))))? true : false ; }
  15. Copy Source | Copy HTML function returnBytes( $v ) { $v = trim( $v ); switch (strtolower( $v [strlen( $v )- 1 ])) { case 'g' : $v *= 1024 ; case 'm' : $v *= 1024 ; case 'k' : $v *= 1024 ; } return $v ; } function checkMemoryForGDUsage( $w , $h , $trueColor = false ){ return (((returnBytes(ini_get( "memory_limit" ))-memory_get_usage())) > ( $w * $h * ( 2 . 5 + (((int) $trueColor ) * 3 ))))? true : false ; }


Actually everything, now the call to the checkMemoryForGDUsage function with the planned widths, heights and palettes, before creating an image, can save (under successful circumstances) from fatal error.

Source: https://habr.com/ru/post/102122/


All Articles