清除并销毁来自imagemagick对象的新克隆,如何?

时间:2022-06-12 03:50:34

This is a model of my code, not the full code... but good enough to ask the following question:

这是我的代码模型,不是完整的代码…但我有足够的理由提出以下问题:

When i create a clone of an imagemagick object, do i have to clear and destroy that object also or just the first one. In other words, in my model code, do i need to also destroy $clone inside the foreach loop or just $im outside the loop, or both ???

当我创建一个imagemagick对象的克隆时,我是否需要清除并销毁这个对象,还是只清除第一个对象。换句话说,在我的模型代码中,我需要在foreach循环中销毁$clone还是在循环外销毁$im ?

Thanks

谢谢

function create_clone($size, $filename, $filepath)
{
   $thumb = array();
   $dir = get_dir($filename); //validate directory to write clones

   if (isset($dir)) {
      $im = new imagick($filepath);

      //create sizes of same image
      foreach ($size as $value) {
         $clone = $im->clone();

         //create clone
      }

      $im->clear();
      $im->destroy();
   }

   return $thumb;
}

1 个解决方案

#1


0  

Imagick::clear() and Imagick::destroy() warp the same ClearMagickWand method, so you only need to call one to free allocated resources. You would need to clear, or destroy, each cloned object within the foreach loop. This is because cloning will only make an exact copy of the object. Clearing original resources will not free identical resources allocated to another object.

Imagick::clear()和Imagick::destroy()使用了相同的ClearMagickWand方法,因此只需调用一个就可以释放分配的资源。您需要清除或销毁foreach循环中的每个克隆对象。这是因为克隆只会对对象进行精确的复制。清除原始资源将不会释放分配给另一个对象的相同资源。

$im = new imagick($filepath);

foreach ($size as $value) {
  $clone = $im->clone();

  // Do work

  $clone->clear();
}

$im->clear();

Also, you should use the clone keyword as the method has been deprecated.

另外,您应该使用克隆关键字作为方法已被弃用。

<?php

  // Initialize image object
  $img = new Imagick($resource);

  // Exact copy of object + data resources
  $imgCopy = clone $img;

  // Compare resource of two objects
  var_dump($img->getImageBlob() === $imgCopy->getImageBlob());
  //=> bool(true)

  // Free original object
  $img->destroy();

  // Verify original object is empty
  var_dump($img->getImageBlob());
  //=> Warning: Uncaught exception 'ImagickException' with message 'Can not process empty Imagick object'

  // Verify copied object's resources are still allocated
  var_dump($imgCopy->getImageBlob());
  //=> string(342) "?PNG\r\n\032\n\000\000\000\rIHDR .... IEND?B`?"

#1


0  

Imagick::clear() and Imagick::destroy() warp the same ClearMagickWand method, so you only need to call one to free allocated resources. You would need to clear, or destroy, each cloned object within the foreach loop. This is because cloning will only make an exact copy of the object. Clearing original resources will not free identical resources allocated to another object.

Imagick::clear()和Imagick::destroy()使用了相同的ClearMagickWand方法,因此只需调用一个就可以释放分配的资源。您需要清除或销毁foreach循环中的每个克隆对象。这是因为克隆只会对对象进行精确的复制。清除原始资源将不会释放分配给另一个对象的相同资源。

$im = new imagick($filepath);

foreach ($size as $value) {
  $clone = $im->clone();

  // Do work

  $clone->clear();
}

$im->clear();

Also, you should use the clone keyword as the method has been deprecated.

另外,您应该使用克隆关键字作为方法已被弃用。

<?php

  // Initialize image object
  $img = new Imagick($resource);

  // Exact copy of object + data resources
  $imgCopy = clone $img;

  // Compare resource of two objects
  var_dump($img->getImageBlob() === $imgCopy->getImageBlob());
  //=> bool(true)

  // Free original object
  $img->destroy();

  // Verify original object is empty
  var_dump($img->getImageBlob());
  //=> Warning: Uncaught exception 'ImagickException' with message 'Can not process empty Imagick object'

  // Verify copied object's resources are still allocated
  var_dump($imgCopy->getImageBlob());
  //=> string(342) "?PNG\r\n\032\n\000\000\000\rIHDR .... IEND?B`?"