Zebra_Form::_convert PHP Method

_convert() private method

Note that this method will update the entries in the {@link $file_upload} property as the converted file will become the "uploaded" file!
private _convert ( string $control, string $type, integer $jpeg_quality = 85, integer $preserve_original_file = false, boolean $overwrite = false ) : boolean
$control string The file upload control's name @param string $type Type to convert an image to. Can be (case-insensitive) JPG, PNG or GIF @param integer $jpeg_quality (Optional) Indicates the quality of the output image (better quality means bigger file size). Range is 0 - 100 Available only if type is "jpg". Default is 85. @param integer $preserve_original_file (Optional) Should the original file be preserved after the conversion is done? Default is FALSE. @param boolean $overwrite (Optional) If a file with the same name as the converted file already exists, should it be overwritten or should the name be automatically computed. If a file with the same name as the converted file already exists and this argument is FALSE, a suffix of "_n" (where n is an integer) will be appended to the file name. Default is FALSE @return boolean Returns TRUE on success or FALSE otherwise @access private
$type string
$jpeg_quality integer
$preserve_original_file integer
$overwrite boolean
return boolean
    private function _convert($control, $type, $jpeg_quality = 85, $preserve_original_file = false, $overwrite = false)
    {
        // make sure the new extension is in lowercase
        $type = strtolower($type);
        // if
        if (isset($this->file_upload[$control]) && isset($this->file_upload[$control]['imageinfo']) && ($type == 'gif' || $type == 'png' || $type == 'jpg')) {
            // get file's current name
            $current_file_name = substr($this->file_upload[$control]['file_name'], 0, strrpos($this->file_upload[$control]['file_name'], '.'));
            // get file's current extension
            $current_file_extension = strtolower(substr($this->file_upload[$control]['file_name'], strrpos($this->file_upload[$control]['file_name'], '.') + 1));
            // if extension is a variation of "jpeg", revert to default "jpg"
            if ($current_file_extension == 'jpeg') {
                $current_file_extension = 'jpg';
            }
            // if new extension is different than the file's current extension
            if ($type != $current_file_extension) {
                // if no overwrite and a file with the same name as the converted file already exists
                if (!$overwrite && is_file($this->file_upload[$control]['path'] . $current_file_name . '.' . $type)) {
                    $suffix = '';
                    // knowing the suffix...
                    // loop as long as
                    while (is_file($this->file_upload[$control]['path'] . $current_file_name . $suffix . '.' . $type)) {
                        // if no suffix was yet set
                        if ($suffix === '') {
                            // start the suffix like this
                            $suffix = '_1';
                        } else {
                            // drop the "_" from the suffix
                            $suffix = str_replace('_', '', $suffix);
                            // increment the suffix
                            $suffix = '_' . ++$suffix;
                        }
                    }
                    // the final file name
                    $current_file_name = $current_file_name . $suffix;
                }
                // if the image transformation class was not already instantiated
                if (!isset($this->Zebra_Image)) {
                    // create a new instance of the image transformation class
                    $this->Zebra_Image = new Zebra_Image();
                }
                // set the source file
                $this->Zebra_Image->source_path = $this->file_upload[$control]['path'] . $this->file_upload[$control]['file_name'];
                // set the target file
                $this->Zebra_Image->target_path = $this->file_upload[$control]['path'] . $current_file_name . '.' . $type;
                // set the quality of the output image (better quality means bigger file size)
                // available only for jpeg files; ignored for other image types
                $this->Zebra_Image->jpeg_quality = $jpeg_quality;
                // if there was an error when resizing the image, return false
                if (!$this->Zebra_Image->resize(0, 0)) {
                    return false;
                }
                // update entries in the file_upload property
                // get the size of the new file
                $this->file_upload[$control]['size'] = filesize($this->Zebra_Image->target_path);
                // update the file name (the file was converted and has a new extension)
                $this->file_upload[$control]['file_name'] = $current_file_name . '.' . $type;
                // get some info about the new file
                $imageinfo = @getimagesize($this->Zebra_Image->target_path);
                // rename some of the attributes returned by getimagesize
                $imageinfo['width'] = $imageinfo[0];
                unset($imageinfo[0]);
                $imageinfo['height'] = $imageinfo[1];
                unset($imageinfo[1]);
                $imageinfo['type'] = $imageinfo[2];
                unset($imageinfo[2]);
                $imageinfo['html'] = $imageinfo[3];
                unset($imageinfo[3]);
                // append image info to the file_upload property
                $this->file_upload[$control]['imageinfo'] = $imageinfo;
                // update the mime type as returned by getimagesize
                $this->file_upload[$control]['type'] = $imageinfo['mime'];
                // if original file is not to be preserved, delete original file
                if (!$preserve_original_file && (!$overwrite || $type != $current_file_extension)) {
                    @unlink($this->Zebra_Image->source_path);
                }
            }
        }
        // if the script gets this far, it means that everything went as planned and we return true
        return true;
    }