imagedestroy

(PHP 4, PHP 5, PHP 7, PHP 8)

imagedestroy β€” Π£Π½ΠΈΡ‡Ρ‚ΠΎΠΆΠ°Π΅Ρ‚ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅

Π’Π½ΠΈΠΌΠ°Π½ΠΈΠ΅

Начиная с PHP 8.5.0 функция УБВАРЕЛА. ΠŸΠΎΠ»Π°Π³Π°Ρ‚ΡŒΡΡ Π½Π° Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ Π½Π°ΡΡ‚ΠΎΡΡ‚Π΅Π»ΡŒΠ½ΠΎ Π½Π΅ Ρ€Π΅ΠΊΠΎΠΌΠ΅Π½Π΄ΡƒΡŽΡ‚.

ОписаниС

#[\Deprecated]
function imagedestroy(GdImage $image): true

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: Ѐункция стала бСсполСзной. Π”ΠΎ PHP 8.0.0 функция Π·Π°ΠΊΡ€Ρ‹Π²Π°Π»Π° рСсурс.

Π”ΠΎ PHP 8.0.0 функция imagedestroy() освобоТдала ΠΏΠ°ΠΌΡΡ‚ΡŒ, ΡΠ²ΡΠ·Π°Π½Π½ΡƒΡŽ с рСсурсом image. Начиная с PHP 8.0.0 ΠΌΠΎΠ΄ΡƒΠ»ΡŒ GD Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚ с ΠΎΠ±ΡŠΠ΅Ρ‚Π°ΠΌΠΈ вмСсто рСсурсов.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

image
ΠžΠ±ΡŠΠ΅ΠΊΡ‚ GdImage, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²Π΅Ρ€Π½ΡƒΠ»Π° функция imagecreatetruecolor() ΠΈΠ»ΠΈ другая функция Π³Π΅Π½Π΅Ρ€Π°Ρ†ΠΈΠΈ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ логичСскоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ true.

Бписок измСнСний

ВСрсия ОписаниС
8.5.0 Ѐункция устарСла.
8.0.0 Ѐункция стала NOP (Π°Π½Π³Π». No Operation) ΠΈ Π½ΠΈ Π½Π° Ρ‡Ρ‚ΠΎ Π½Π΅ влияСт.
8.0.0 ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ image Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ GdImage; Ρ€Π°Π½ΡŒΡˆΠ΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π» ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½Ρ‹ΠΉ gd-рСсурс (resource).

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ уничтоТСния изобраТСния Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ imagedestroy() Π΄ΠΎ PHP 8.0.0

<?php

// Π‘ΠΎΠ·Π΄Π°Ρ‘ΠΌ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Ρ€Π°Π·ΠΌΠ΅Ρ€ΠΎΠΌ 100 Π½Π° 100 пиксСлСй
$im = imagecreatetruecolor(100, 100);

// ИзмСнСниС ΠΈΠ»ΠΈ сохранСниС изобраТСния

// ОсвобоТдаСм ΠΏΠ°ΠΌΡΡ‚ΡŒ
imagedestroy($im);
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 7 notes

up
56
Docey ΒΆ
19 years ago
When the script stop PHP will automatic destory ANY
resources, this goes for aswell for images, thus in the
case the use clicks the stop button, php will automatic
clear the resource.

thus imagedestroy is used to clear the memory BEFORE
the script ends. this is usefull to keep memory usage
DURING the script to an acceptable level.

hope this clear somethings up.
up
7
devel at kijote dot com dot ar ΒΆ
6 years ago
Important note: When the imagedestroy is called, the resource is freed but the printed resource output is the same as before calling the function:

<?php

  $img = imagecreate(1, 1);

  print_r([$img, $img ? 'TRUE': 'FALSE', is_resource($img) ? 'TRUE' : 'FALSE', get_resource_type($img) ?: 'FALSE']);

  /*
  Result:
  Array
  (
    [0] => Resource id #1
    [1] => TRUE
    [2] => TRUE
    [3] => gd

  )
  */

  imagedestroy($img);

  print_r([$img, $img ? 'TRUE': 'FALSE', is_resource($img) ? 'TRUE' : 'FALSE', get_resource_type($img) ?: 'FALSE']);

  /*
  Result:
  Array
  (
    [0] => Resource id #1
    [1] => TRUE
    [2] => FALSE
    [3] => Unknown
  )
  */
?>

As you can see in the above example, the first index in array is TRUE in both cases. So, despite the common thinking you can't trust in something like:

<?php

  if ($img)  {  // it's casted as boolean and returns true even after imagedestroy is called
     // do something
  }

?>

If you need to ensure the availability of a certain resource, you must to use is_resource and get_resource_type functions.
up
18
Claude D. ΒΆ
10 years ago
Caution should be observed with imagedestroy(); copying your reference variable over to another variable will cause imagedestroy to destroy both at once.

Eg.: 

$a = imagecreate(...);
$b = $a;
imagedestroy($a);

While you'd think $b still contains your image, it doesn't. Both $a and $b are destroyed.
up
5
dan at mlodecki dot net ΒΆ
22 years ago
I have noticed that gd drawing functions can behave oddly if there is a previous image which has not been imagedestroy()'ed.  You should always imagedestroy when you are done with an image object.
up
6
Andrew Hoffmann - ahoffmann at wisc dot edu ΒΆ
20 years ago
When working with a lot of high-resolution images, it's IMPERATIVE that you use the imagedestroy() function.

In my scenario, I was taking two high resolution desktop wallpapers and shrinking them down into successively smaller ones (preventing the user from having to upload a dozen files).

At first, my script would run, then just stop.  I realized later that I had not destroyed the source image and the newly resized image in memory after I had finished writing the file out to disk.  Thus, I quickly reached the memory limit that my hosting provider placed in their php.ini file.

Reusing an image variable does NOT clear the old data out of memory!  You must use imagedestroy() to clear the data out.  (I don't know if unset() works as well).

Also note that the image data in memory is raw, so don't base how much memory you are using based on the original filesize of the compressed image (such as jpeg or png).
up
4
corpus-deus at softhome dot net ΒΆ
16 years ago
In theory creating an image object and calling imagedestroy in your destructor should be a good way of doing it; something like:

<?php
final class My_Image() {

  private $img;

  public function __construct() {
    $this->img = imagecreatetruecolor();
    // ... other stuff ...
  }

  public function __destruct() {
    if(is_resource($this->img)) {
      imagedestroy($this->img);
    }
  }

  // ... other methods...

}
?>

I check that $this->img is a resource in case imagecreatetruecolor() fails or you null the value somewhere down the line from a method call meaning that $this->img is NOT a resource, in which case imagedestroy is an unecessary function call that just fails with a warning message.
up
1
webmaster at codefisher dot org ΒΆ
18 years ago
And to continue what Docey said, if php did not destroy all resources when the script stopped it would be a huge memory leak and everyone would be crying out for it to be fixed right away.

I have been using this function during a script that was breaking an image made of many little icons into the little parts, which could mean 400+ images in the one script, which was using a lot of memory so I needed to destroy them.