hoangsoft90
3/22/2015 - 4:23 PM

prestashop-categories-second-image

protected function uploadImage($id, $name, $dir, $ext = false, $width = null, $height = null)
{
    if (isset($_FILES[$name]['tmp_name']) && !empty($_FILES[$name]['tmp_name']))
    {
        // Delete old image
        if (Validate::isLoadedObject($object = $this->loadObject()))
            $object->deleteImage();
        else
            return false;
 
        // Check image validity
        $max_size = isset($this->max_image_size) ? $this->max_image_size : 0;
        if ($error = ImageManager::validateUpload($_FILES[$name], Tools::getMaxUploadSize($max_size)))
            $this->errors[] = $error;
 
        $tmp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS');
        if (!$tmp_name)
            return false;
 
        if (!move_uploaded_file($_FILES[$name]['tmp_name'], $tmp_name))
            return false;
 
        // Evaluate the memory required to resize the image: if it's too much, you can't resize it.
        if (!ImageManager::checkImageMemoryLimit($tmp_name))
            $this->errors[] = Tools::displayError('Due to memory limit restrictions, this image cannot be loaded. Please increase your memory_limit value via your server\'s configuration settings. ');
 
        // Copy new image
        if (empty($this->errors) && !ImageManager::resize($tmp_name, _PS_IMG_DIR_.$dir.$id.'.'.$this->imageType, (int)$width, (int)$height, ($ext ? $ext : $this->imageType)))
            $this->errors[] = Tools::displayError('An error occurred while uploading the image.');
 
        if (count($this->errors))
            return false;
        if ($this->afterImageUpload())
        {
            unlink($tmp_name);
            return true;
        }
        return false;
    }
    return true;
}