Slim Image Cropper PHP 8

Used to use SlimImageCropper to crop images before uploading on websites.
I get a 500 server error using php 8.

I know nothing about php classes or what could be causing the error with a newer version of php

the error is in the following code:

abstract class SlimStatus {
    const FAILURE = 'failure';
    const SUCCESS = 'success';
}

class Slim {

    public static function getImages($inputName = 'slim') {

        $values = Slim::getPostData($inputName);

        // test for errors
        if ($values === false) {
            return false;
        }

        // determine if contains multiple input values, if is singular, put in array
        $data = array();
        if (!is_array($values)) {
            $values = array($values);
        }

        // handle all posted fields
        foreach ($values as $value) {
            $inputValue = Slim::parseInput($value);
            if ($inputValue) {
                array_push($data, $inputValue);
            }
        }

        // return the data collected from the fields
        return $data;

    }

    // $value should be in JSON format
    private static function parseInput($value) {

        // if no json received, exit, don't handle empty input values.
        if (empty($value)) {return null;}

        // If magic quotes enabled
        if (get_magic_quotes_gpc()) {
            $value = stripslashes($value);
        }

        // The data is posted as a JSON String so to be used it needs to be deserialized first
        $data = json_decode($value);

        // shortcut
        $input = null;
        $actions = null;
        $output = null;
        $meta = null;

        if (isset ($data->input)) {

            $inputData = null;
            if (isset($data->input->image)) {
                $inputData = Slim::getBase64Data($data->input->image);
            }
            else if (isset($data->input->field)) {
                $filename = $_FILES[$data->input->field]['tmp_name'];
                if ($filename) {
                    $inputData = file_get_contents($filename);
                }
            }

            $input = array(
                'data' => $inputData,
                'name' => $data->input->name,
                'type' => $data->input->type,
                'size' => $data->input->size,
                'width' => $data->input->width,
                'height' => $data->input->height,
            );

        }

        if (isset($data->output)) {

            $outputDate = null;
            if (isset($data->output->image)) {
                $outputData = Slim::getBase64Data($data->output->image);
            }
            else if (isset ($data->output->field)) {
                $filename = $_FILES[$data->output->field]['tmp_name'];
                if ($filename) {
                    $outputData = file_get_contents($filename);
                }
            }

            $output = array(
                'data' => $outputData,
                'name' => $data->output->name,
                'type' => $data->output->type,
                'width' => $data->output->width,
                'height' => $data->output->height
            );
        }

        if (isset($data->actions)) {
            $actions = array(
                'crop' => $data->actions->crop ? array(
                    'x' => $data->actions->crop->x,
                    'y' => $data->actions->crop->y,
                    'width' => $data->actions->crop->width,
                    'height' => $data->actions->crop->height,
                    'type' => $data->actions->crop->type
                ) : null,
                'size' => $data->actions->size ? array(
                    'width' => $data->actions->size->width,
                    'height' => $data->actions->size->height
                ) : null,
                'rotation' => $data->actions->rotation,
                'filters' => $data->actions->filters ? array(
                    'sharpen' => $data->actions->filters->sharpen
                ) : null
            );
        }

        if (isset($data->meta)) {
            $meta = $data->meta;
        }

        // We've sanitized the base64data and will now return the clean file object
        return array(
            'input' => $input,
            'output' => $output,
            'actions' => $actions,
            'meta' => $meta
        );
    }

    // $path should have trailing slash
    public static function saveFile($data, $name, $path = 'tmp/', $uid = true) {

        // Add trailing slash if omitted
        if (substr($path, -1) !== '/') {
            $path .= '/';
        }

        // Test if directory already exists
        if(!is_dir($path)){
            mkdir($path, 0755, true);
        }

        // Sanitize characters in file name
        $name = Slim::sanitizeFileName($name);

        // Let's put a unique id in front of the filename so we don't accidentally overwrite other files
        if ($uid) {
            $name = uniqid() . '_' . $name;
        }

        // Add name to path, we need the full path including the name to save the file
        $path = $path . $name;

        // store the file
        Slim::save($data, $path);

        // return the files new name and location
        return array(
            'name' => $name,
            'path' => $path
        );
    }

    /**
     * Get data from remote URL
     * @param $url
     * @return string
     */
    public static function fetchURL($url, $maxFileSize) {
        if (!ini_get('allow_url_fopen')) {
            return null;
        }
        $content = null;
        try {
            $content = @file_get_contents($url, false, null, 0, $maxFileSize);
        } catch(Exception $e) {
            return false;
        }
        return $content;
    }

    public static function outputJSON($data) {
        header('Content-Type: application/json');
        echo json_encode($data);
    }

    /**
     * http://stackoverflow.com/a/2021729
     * Remove anything which isn't a word, whitespace, number
     * or any of the following characters -_~,;[]().
     * If you don't need to handle multi-byte characters
     * you can use preg_replace rather than mb_ereg_replace
     * @param $str
     * @return string
     */
    public static function sanitizeFileName($str) {
        // Basic clean up
        $str = preg_replace('([^\w\s\d\-_~,;\[\]\(\).])', '', $str);
        // Remove any runs of periods
        $str = preg_replace('([\.]{2,})', '', $str);
        return $str;
    }

    /**
     * Gets the posted data from the POST or FILES object. If was using Slim to upload it will be in POST (as posted with hidden field) if not enhanced with Slim it'll be in FILES.
     * @param $inputName
     * @return array|bool
     */
    private static function getPostData($inputName) {

        $values = array();

        if (isset($_POST[$inputName])) {
            $values = $_POST[$inputName];
        }
        else if (isset($_FILES[$inputName])) {
            // Slim was not used to upload this file
            return false;
        }

        return $values;
    }

    /**
     * Saves the data to a given location
     * @param $data
     * @param $path
     * @return bool
     */
    private static function save($data, $path) {
        if (!file_put_contents($path, $data)) {
            return false;
        }
        return true;
    }

    /**
     * Strips the "data:image..." part of the base64 data string so PHP can save the string as a file
     * @param $data
     * @return string
     */
    private static function getBase64Data($data) {
        return base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $data));
    }

}

Do you have access to the PHP logs? It would be good to know what the actual error is that’s happening. Otherwise we can only blindly guess what might be wrong. And blindly guessing doesn’t tend to yield good results.

One thing I spotted is that you use the function get_magic_quotes_gpc which was deprecated in version 7.4.0 and removed in 8.0.0.

1 Like

Stack trace:

#0 D:\Web\arcadeoperator\account\server\slim.php(27): Slim::parseInput('{"server":null,...')
#1 D:\Web\arcadeoperator\account\server\asyncenc.php(5): Slim::getImages()
#2 {main}
  thrown in D:\Web\arcadeoperator\account\server\slim.php on line 45
[13-Jan-2024 15:54:02 UTC] PHP Warning:  Undefined variable $images in D:\Web\arcadeoperator\account\server\asyncenc.php on line 15
[13-Jan-2024 15:54:02 UTC] PHP Stack trace:
[13-Jan-2024 15:54:02 UTC] PHP   1. {main}() D:\Web\arcadeoperator\account\server\asyncenc.php:0
[13-Jan-2024 15:54:02 UTC] PHP Fatal error:  Uncaught TypeError: array_shift(): Argument #1 ($array) must be of type array, null given in D:\Web\arcadeoperator\account\server\asyncenc.php:23
Stack trace:
#0 D:\Web\arcadeoperator\account\server\asyncenc.php(23): array_shift(NULL)
#1 {main}
  thrown in D:\Web\arcadeoperator\account\server\asyncenc.php on line 23
[13-Jan-2024 15:56:06 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function get_magic_quotes_gpc() in D:\Web\arcadeoperator\account\server\slim.php:45
Stack trace:
#0 D:\Web\arcadeoperator\account\server\slim.php(27): Slim::parseInput('{"server":null,...')
#1 D:\Web\arcadeoperator\account\server\asyncenc.php(5): Slim::getImages()
#2 {main}
  thrown in D:\Web\arcadeoperator\account\server\slim.php on line 45
[13-Jan-2024 15:56:56 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function get_magic_quotes_gpc() in D:\Web\arcadeoperator\account\server\slim.php:45
Stack trace:
#0 D:\Web\arcadeoperator\account\server\slim.php(27): Slim::parseInput('{"server":null,...')
#1 D:\Web\arcadeoperator\account\server\asyncenc.php(5): Slim::getImages()
#2 {main}
  thrown in D:\Web\arcadeoperator\account\server\slim.php on line 45

So if i remove this line it could possibly work??

You can’t simply remove a function call or a line of code and hope it will work as it did before. You need to check the manual and see what the function did and what the proper way is to handle things.

However, in this case the function has performed no useful role for some while. If you remove these three lines of code

if (get_magic_quotes_gpc()) {
    $value = stripslashes($value);
}

it should be fine. If you look at the manual, you will see that the function returned false, so those 3 lines have been redundant for some while.

1 Like

Spot on.

I removed the check and it is working normally as it did before.

Many thanks!!!

1 Like