/** * Função para redimensionar imagens * @param ($_FILE) $tmp_file - Arquivo que veio no post do form * @param (string) $dest_img - Destino da imagem * @param (int) $maxW - Largura máxima * @param (int) $maxH - Altura máxima * @return (bool) - Sucesso */ public static function redimensionar_imagem($tmp_file, $dest_img, $maxW, $maxH, $keepRatio = true) { if (!is_file($tmp_file['tmp_name'])) { vdie($tmp_file['tmp_name'] . ' não existe.'); } if ($keepRatio) { // Dimensões originais list($width, $height) = getimagesize($tmp_file['tmp_name']); // Limites $max_width = $maxW > 0 ? $maxW : $width; $max_height = $maxH > 0 ? $maxH : $height; // Novas dimensões list($new_height, $new_width) = MY_Utils::calcular_novas_dimensoes($height, $width, $max_height, $max_width); $new_width = $maxW > 0 ? $maxW : $max_width; $new_height = $maxH > 0 ? $maxH : $max_heigth; } else { // Dimensões originais list($width, $height) = getimagesize($tmp_file['tmp_name']); // Limites $max_width = $maxW > 0 ? $maxW : $width; $max_height = $maxH > 0 ? $maxH : $height; // Novas dimensões list($new_height, $new_width) = MY_Utils::calcular_novas_dimensoes_2($height, $width, $max_height, $max_width); } // Converte/redimensiona $tmp_img = $tmp_file['tmp_name']; if (copy($tmp_img, $dest_img) && is_file($tmp_img)) { // Chama o arquivo com a classe WideImage require_once 'WideImage/lib/WideImage.php'; // Carrega a imagem a ser manipulada $image = WideImage::load($tmp_img); // Redimensiona a imagem $image = $image->resize($new_width, $new_height); if (!$keepRatio) { // Corta a imagem $image = $image->crop(0, 0, $maxW, $maxH); } // JPG to progressive if (array_pop(explode('.', $dest_img)) == 'jpg') { imageinterlace($image->getHandle(), true); } // Salva a imagem em um arquivo (novo ou não) $image->saveToFile($dest_img); } else { vdie('Não foi possível gravar a imagem no servidor.'); } // Se ok $ok = is_file($dest_img); if (!$ok) { vdie('Não foi possível receber a imagem no servidor.'); } return $ok; }