Title: Replace GD resize function with Imagemagick.
Last modified: August 21, 2016

---

# Replace GD resize function with Imagemagick.

 *  [mxfarsa](https://wordpress.org/support/users/mxfarsa/)
 * (@mxfarsa)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/replace-gd-resize-function-with-imagemagick/)
 * Hi everyone,
 * This issue may not be easy to fix, I do not know but here it is.
 * I am selling photos online to different newspapers, they buy and download one
   image or more with a size (Large, medium or small) when selected the site resizes
   the image and make it available for download – so far everything is working perfectly
   and as it should. But and this is a big but, when it resizes the image all exif
   and metadata is lost – som instead of a image with correct colorprofile, meta
   data, and resoultion, the customer gets a image in 96ppi (not 300ppi) that they
   bought….
 * The shopping solution I use are doing the resize and using wordpress GD library
   and when the code “imagecopyresampled” is used that is when metadata and exif
   gets lost.
 * So to test out if using imagemagick does a better job of keeping the data i now
   need to change the code to use imagemagick instead of GD library.
 * I am not a programmer so here is the original code of the resize function:
 *     ```
       public function create_download_size( $size, $location=null ){
   
               $image_p = imagecreatetruecolor( $size['width'], $size['height'] );
               $image = imagecreatefromjpeg( $location );
   
               list( $current_image['width'], $current_image['height'] ) = getimagesize( $location );
               imagecopyresampled($image_p, $image, 0, 0, 0, 0, $size['width'], $size['height'], $current_image['width'], $current_image['height']);
   
               $destination_file = $this->tmp_dir . basename( $location );
   
               if ( ! file_exists( $destination_file ) ){
                   wp_mkdir_p( dirname( $destination_file ) );
               }
       ```
   
 * _[Moderator Note: Please post code & markup between backticks or use the code
   button. Your posted code may now have been permanently damaged by the forum’s
   parser.]_
 * Now one thing the function must leave the original image alone and create a new
   size temporarily for the download.
 * How do I switch this function to use imagemagick instead of GD for the same operation?

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/replace-gd-resize-function-with-imagemagick/#post-4343152)
 * I’m not 100% sure this is the solution, this essentially takes GD out of play,
   forcing ImageMagick to be used. If your function uses the editor object used 
   by WP, this should work, but it’s not clear that is what’s happening. Easy enough
   to try and delete if it doesn’t work. Copy this code into a new empty PHP file
   and save it in wp-content/plugins/. Activate this “Force Imagick” plugin.
 *     ```
       <?php
       /**
        * Plugin Name: Force Imagick
        * Description: Stops GD image library from being used by default
        * Author: bcworkz
        * License: GPL2
        */
       add_filter('wp_image_editors', 'fi_force_imagick');
       function fi_force_imagick() {
          return array('WP_Image_Editor_Imagick');
       }
       ```
   
 *  Thread Starter [mxfarsa](https://wordpress.org/support/users/mxfarsa/)
 * (@mxfarsa)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/replace-gd-resize-function-with-imagemagick/#post-4343157)
 * Thank you bcworkz,
 * I will test this out. I hope it will work..
 * other than that if this does not work, can I switch out the “imagecopyresampled”
   function for imagicks resize function directly in plugin file.
 * From what I have been told, the resize function in imagick, does not strip away
   the exif of image. The code I posted uses imagecopyresampled, but I want it to
   use imagick:resize instead.
 *     ```
       public function create_download_size( $size, $location=null ){
   
               $image_p = imagecreatetruecolor( $size['width'], $size['height'] );
               $image = imagecreatefromjpeg( $location );
   
               list( $current_image['width'], $current_image['height'] ) = getimagesize( $location );
               imagecopyresampled($image_p, $image, 0, 0, 0, 0, $size['width'], $size['height'], $current_image['width'], $current_image['height']);
   
               $destination_file = $this->tmp_dir . basename( $location );
   
               if ( ! file_exists( $destination_file ) ){
                   wp_mkdir_p( dirname( $destination_file ) );
               }
       ```
   
 * Regards, mxfarsa
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/replace-gd-resize-function-with-imagemagick/#post-4343176)
 * Looking at this code again, I really doubt the WP editor is being used, so my
   little plugin idea will not work. I’m not sure what you’re referring to by `imagick:
   resize`, perhaps `Imagick::resizeImage()`? That’s worth a try if your PHP installation
   has Imagck module loaded. Or try the WP version: `WP_Image_Editor_Imagick::resize()`.
   The parameters are different than `imagecopyresampled()`, some rearranging will
   be necessary.
 *  Thread Starter [mxfarsa](https://wordpress.org/support/users/mxfarsa/)
 * (@mxfarsa)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/replace-gd-resize-function-with-imagemagick/#post-4343179)
 * Thank you for your answer bcworkz, and yes when I tried the plugin version it
   did not work.
 * So I need to alter the file to use imagemagick instead…. I´ll have to read up
   on php and these parts before i give it a try.
 * But right now the three parts i need to rewrite is
 *     ```
       $image_p = imagecreatetruecolor( $size['width'], $size['height'] );
               $image = imagecreatefromjpeg( $location );
   
               list( $current_image['width'], $current_image['height'] ) = getimagesize( $location );
               imagecopyresampled($image_p, $image, 0, 0, 0, 0, $size['width'], $size['height'], $current_image['width'], $current_image['height']);
       ```
   
 * 1. imagecreatetruecolor – need change
    2. imagecreatefromjpeg – need change 3.
   imagecopyresampled – need change
 * these are all calling GD functions, but I need to read up on what functions I
   should call to use imagick instead.
 * The plugin gets the size variable from cart when a buyer selects one of three
   choices (large, medium, small) – upon checkout a resized copy of the image is
   created and put in a tmp dir, the original is not altered.
 * So the different sizes are only created temporaily when bought – not on upload,
   but how do I initate imagicks functions with the variables that are already in
   the file, because all the paths and locations will remain untouched.
 * The only function I need is to resize an exact copy of original in a smaller 
   size, and make sure that the resized image still have all exif info unaltered.
 * Alot of questions here, and I am not in any way good with php code, but I will
   test after reading the php manual.
 * Or do you have some more information on how to initiate the imagick class and
   execute.
 * My webhotel has imagick installed so it should not be any problem on the server
   end.
 * I value your help bcworkz
    Regards, mxfarsa
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/replace-gd-resize-function-with-imagemagick/#post-4343193)
 * Your primary reference will be [http://us2.php.net/manual/en/book.imagick.php](http://us2.php.net/manual/en/book.imagick.php),
   which you’ve probably found already, but just in case. There are examples of 
   initiating the class and calling methods.
 * The snippet you posted looks like in may be from within a class definition, is
   this the case? I’m unsure how to use one class from within another. I’m sort 
   of new to OOP, I know you are.
 * Somewhat of an issue is what ever changes you make would likely be lost when 
   you upgrade this software. A proper approach will minimize what needs to be done
   to reinstate your modifications after an upgrade. It may be best to extend this
   class and override this one function. Maybe save that for later and first just
   do what’s easiest to confirm this is going to work at all.
 * I believe you should be able to simply substitute in the Imagick function you
   need, including the Imagick scope resolution (the `Imagick::` part). Rearrange
   the parameters to work with the new function. I believe this is how to reference
   one class function from within another, but as I warned, OOP is new to me as 
   well. You can’t always use class functions this way and I don’t know the reasoning
   of when you can and cannot. My best guess is it has to do with if `this` is used
   or not.
 * I’m pretty much up against the limits of my knowledge, but I will try to continue
   answering any questions you may have. In the mean time, happy learning, it will
   serve you well into the future.
 *  Thread Starter [mxfarsa](https://wordpress.org/support/users/mxfarsa/)
 * (@mxfarsa)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/replace-gd-resize-function-with-imagemagick/#post-4343201)
 * Thank you bcworkz, I will read up on this at php manual – and I will test around
   to see if I get it to work…
 * I´ll post questions along the way 🙂
 * //mxfarsa

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Replace GD resize function with Imagemagick.’ is closed to new replies.

## Tags

 * [exif](https://wordpress.org/support/topic-tag/exif/)
 * [function](https://wordpress.org/support/topic-tag/function/)
 * [imagemagick](https://wordpress.org/support/topic-tag/imagemagick/)
 * [resize](https://wordpress.org/support/topic-tag/resize/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 6 replies
 * 2 participants
 * Last reply from: [mxfarsa](https://wordpress.org/support/users/mxfarsa/)
 * Last activity: [12 years, 6 months ago](https://wordpress.org/support/topic/replace-gd-resize-function-with-imagemagick/#post-4343201)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
