Need to change from ImageMagick to GD

Robert9

Well-known member
I use an extern script to manipulate pictures from my forum.
I get a picture with curl.

Now i need to change the script to use GD instead of ImageMagick.
These are the parts i have to exchange (there is more code, but not neccessary to show here)

Code:
    $i = new \Imagick();
    $i->readImage($newFile);

    // crop the image
   ... more code ...

    // thumbnail the image
    $i->ThumbnailImage($width,$height,true);

    $i->writeImage($newFile);


Has someone an idea or a site with examples, how i can exchange this imgaeMagick-code to GD, please?
I need to create an image from the file ($newFile); i need to "thumbnail it", i need to save it.
 
Code:
    $data=curl_exec($ch);


    if (!file_exists($newFile))
    {
        // create blank file
        touch($newFile);
        chmod($newFile, octdec("0666"));
    }

    // put file
    file_put_contents($newFile, $data);


    $imageDetails = @getimagesize($newFile);

    // get image specifics
    $widthImg = $imageDetails[0];
    $heightImg = $imageDetails[1];
    $imageType = $imageDetails[2];
    $imageMime = $imageDetails['mime'];

    $width=225;
    $height=300;

    $image = @imagecreatefromjpeg($newFile);

    // crop the image
    if(($widthImg/$width) < ($heightImg/$height))
    {
        $imageNew = imagecrop($image, ['x' => 0,
                                       'y' => (($heightImg-($height*$widthImg/$width))/2),
                                       'width' => $widthImg,
                                       'height' => floor($height*$widthImg/$width)
                                       ]
                              );
    }
    else
    {
        $imageNew = imagecrop($image, ['x' => (($widthImg-($width*$heightImg/$height))/2),
                                       'y' => 0,
                                       'width' => ceil($width*$heightImg/$height),
                                       'height' => $heightImg
                                       ]
                              );
    }

    // thumbnail the image
    $imgResized = imagescale($imageNew , $width, $height);
    imagejpeg($imgResized, $newFile);


Seems this is not finished, but half way ...
 
Last edited:
Normally we have only jpg, but sometimes we have a png ...

Code:
    if ($imageMime == 'image/png')

        $image = @imagecreatefrompng($newFile);

    else

        $image = @imagecreatefromjpeg($newFile);
 
Back
Top Bottom