Example #1
0
<?php

require_once '../libs/NWD/NwdGD.2.class.php';
$file = $_FILES['foto_field'];
$imgName = $file['name'];
$extensao = strtolower(strrchr($imgName, '.'));
$anexoNb = $_POST['anexoNb'];
if (strstr('*.jpg;*.jpeg;*.gif;*.png', $extensao)) {
    //if($anexoNb == '01'){ $imgName = 'PRINCIPAL_'.$imgName; }
    $nImageName = preg_replace('/[^a-z0-9.]/i', '_', utf8_decode($imgName));
    if (move_uploaded_file($file['tmp_name'], '../_temp/' . $nImageName)) {
        NwdGD::imgResize('../_temp/' . $nImageName, '../_temp/' . $nImageName, 800, 600, 'proporc', 'ffffff', 100, true);
        echo '<script>window.top.window.finishUploadImages02(\'' . $nImageName . '\')</script>';
    }
}
 public static function imgResize($imgInput, $imgOutput, $largImg, $altImg, $imgProporc, $corBg = 'ffffff', $imgQualidade = 100, $imgMenor = false, $marcadagua = array())
 {
     //= '', $marcaBottom = 10, $marcaRight = 10
     $extensao = strtolower(strrchr($imgInput, '.'));
     if (strstr('*.jpg;*.jpeg;*.gif;*.png', $extensao)) {
         list($largImgOrigin, $altImgOrigin) = getimagesize($imgInput);
         if ($imgMenor && ($largImgOrigin <= $largImg && $altImgOrigin <= $altImg)) {
             $newLargImg = $largImgOrigin;
             $newAltImg = $altImgOrigin;
         } else {
             $newLargImg = round($largImgOrigin * $altImg / $altImgOrigin);
             $newAltImg = round($altImgOrigin * $largImg / $largImgOrigin);
         }
         switch ($imgProporc) {
             case 'proporc':
                 if ($newLargImg > $largImg) {
                     $newLargImg = $largImg;
                 }
                 if ($newAltImg > $altImg) {
                     $newAltImg = $altImg;
                 }
                 $pontoX = 0;
                 $pontoY = 0;
                 $largImg = $newLargImg;
                 $altImg = $newAltImg;
                 break;
             case 'crop':
                 if ($newLargImg < $largImg) {
                     $newLargImg = $largImg;
                 }
                 if ($newAltImg < $altImg) {
                     $newAltImg = $altImg;
                 }
                 $pontoX = ($largImg - $newLargImg) / 2;
                 $pontoY = ($altImg - $newAltImg) / 2;
                 break;
             case 'fixo':
                 if ($newLargImg > $largImg) {
                     $newLargImg = $largImg;
                 }
                 if ($newAltImg > $altImg) {
                     $newAltImg = $altImg;
                 }
                 $pontoX = ($largImg - $newLargImg) / 2;
                 $pontoY = ($altImg - $newAltImg) / 2;
                 break;
             case 'estend':
                 $newLargImg = $largImg;
                 $newAltImg = $altImg;
                 $pontoX = 0;
                 $pontoY = 0;
                 break;
         }
         $newImg = imagecreatetruecolor($largImg, $altImg);
         switch ($extensao) {
             case '.jpg':
             case '.jpeg':
                 //--------------------------------------------------------------------------------------------
                 // 	cor de fundo da imagem
                 //--------------------------------------------------------------------------------------------
                 $Bg = NwdGD::HexToRGB($corBg);
                 $imgBgColor = imagecolorallocate($newImg, $Bg['red'], $Bg['green'], $Bg['blue']);
                 imagefill($newImg, 0, 0, $imgBgColor);
                 //--------------------------------------------------------------------------------------------
                 $imgSource = imagecreatefromjpeg($imgInput);
                 imagecopyresampled($newImg, $imgSource, $pontoX, $pontoY, 0, 0, $newLargImg, $newAltImg, $largImgOrigin, $altImgOrigin);
                 if ($marcadagua) {
                     NwdGD::insertWatermark($marcadagua, $newImg, $newLargImg, $newAltImg, $marcaRight, $marcaBottom);
                 }
                 $Copy = imagejpeg($newImg, $imgOutput, $imgQualidade);
                 break;
             case '.gif':
                 //--------------------------------------------------------------------------------------------
                 // 	cor de fundo da imagem
                 //--------------------------------------------------------------------------------------------
                 $Bg = NwdGD::HexToRGB($corBg);
                 $imgBgColor = imagecolorallocate($newImg, $Bg['red'], $Bg['green'], $Bg['blue']);
                 imagefill($newImg, 0, 0, $imgBgColor);
                 //--------------------------------------------------------------------------------------------
                 $imgSource = imagecreatefromgif($imgInput);
                 imagecopyresampled($newImg, $imgSource, $pontoX, $pontoY, 0, 0, $newLargImg, $newAltImg, $largImgOrigin, $altImgOrigin);
                 if ($marcadagua) {
                     NwdGD::insertWatermark($marcadagua, $newImg, $newLargImg, $newAltImg, $marcaRight, $marcaBottom);
                 }
                 $Copy = imagegif($newImg, $imgOutput, $imgQualidade);
                 break;
             case '.png':
                 //--------------------------------------------------------------------------------------------
                 // 	cor de fundo e alpha da imagem
                 //--------------------------------------------------------------------------------------------
                 $Bg = NwdGD::HexToRGB($corBg);
                 imagealphablending($newImg, false);
                 imagesavealpha($newImg, true);
                 $imgBgColorAlpha = imagecolorallocatealpha($newImg, $Bg['red'], $Bg['green'], $Bg['blue'], 127);
                 // 127 = alfa 0
                 imagefill($newImg, 0, 0, $imgBgColorAlpha);
                 //--------------------------------------------------------------------------------------------
                 $imgSource = imagecreatefrompng($imgInput);
                 imagecopyresampled($newImg, $imgSource, $pontoX, $pontoY, 0, 0, $newLargImg, $newAltImg, $largImgOrigin, $altImgOrigin);
                 if (count($marcadagua) > 0) {
                     NwdGD::insertWatermark($marcadagua, $newImg, $newLargImg, $newAltImg, $marcaRight, $marcaBottom);
                 }
                 $Copy = imagepng($newImg, $imgOutput);
                 break;
         }
         imagedestroy($imgSource);
         imagedestroy($newImg);
     }
     if (@$Copy) {
         return true;
     } else {
         return false;
     }
 }
 $imgMenor = @$values[5] ? $values[5] : false;
 $limitName = 100;
 $pastaDestino = '../../../imagens/fotos/';
 $imageTemp = $pastaDestino . '_temp/' . $img['imagem'];
 $thumbTemp = $pastaDestino . '_temp/thumbs/' . $img['imagem'];
 $animal = mysql_fetch_assoc(mysql_query("SELECT * FROM `animais` WHERE `id` = '{$idanimal}' LIMIT 1"));
 $newName = $animal['nome'] . '_' . $animal['cientifico'] . '_' . $animal['maisnomes'];
 $newName = Strings::stringToUrl($newName);
 $newName = strtolower(substr($newName, 0, $limitName - 8) . '.' . $extensao);
 $newName = NWD::autoRename($pastaDestino . 'imgs', $newName);
 $altaOutput = $pastaDestino . 'alta/' . $newName;
 $imageOutput = $pastaDestino . 'imgs/' . $newName;
 $thumbOutput = $pastaDestino . 'thumbs/' . $newName;
 $copy = copy($imageTemp, $altaOutput);
 $imagem = NwdGD::imgResize($altaOutput, $imageOutput, $values[0], $values[1], $values[2], $corBg, $imgQualidade, $imgMenor);
 $thumb = NwdGD::imgResize($altaOutput, $thumbOutput, $values[6], $values[7], $values[8], $corBg, $imgQualidade, $imgMenor);
 if ($imagem != false) {
     $lastPos = mysql_fetch_assoc(mysql_query("SELECT `pos` FROM `fotos` WHERE `id_animal` = '{$idanimal}' ORDER BY `pos` DESC LIMIT 1"));
     $pos = $lastPos['pos'] + 1;
     $data = implode('-', array_reverse(explode('/', $data))) . ' ' . $time;
     $insert = mysql_query("INSERT INTO `fotos` (`pos`, `id_animal`, `id_cidade`, `id_autor`, `imagem`, `data`, `tecnica`, `descricao`, `ativo`) \n\t\t\t\t\t\t\t\t\t\t\t\t\tVALUES ('{$pos}', '{$idanimal}', '{$cidade}', '{$idautor}', '{$newName}', '{$data}', '{$tecnica}', '{$descricao}', '1')");
 }
 if (@$insert) {
     if (@$tempClear) {
         $clear = mysql_query("DELETE FROM fotos_temp WHERE id = '{$idItem}' LIMIT 1");
         @unlink($imageTemp);
         @unlink($thumbTemp);
     } else {
         $clear = mysql_query("UPDATE `fotos_temp` SET `id_publicado` = '" . mysql_insert_id() . "' WHERE `id` = '{$idItem}' LIMIT 1");
     }
     if ($clear) {
 public static function copyResize($file, $pastaDestino, $values, $limitName = 20, $imgName = '', $watermark = '')
 {
     $result = false;
     if (is_file($file)) {
         $largImg = $values[0];
         $altImg = $values[1];
         $imgProporc = $values[2];
         $corBg = @$values[3] ? $values[3] : 'ffffff';
         $imgQualidade = @$values[4] ? $values[4] : 100;
         $imgMenor = @$values[5] ? $values[5] : false;
         $largThumb = @$values[6];
         $altThumb = @$values[7];
         $thumbProporc = @$values[8];
         $geraThumb = $largThumb && $altThumb && $thumbProporc ? true : false;
         // se possuir todas as especificações, gera a miniatura
         $fileName = basename($file);
         $extensao = strtolower(strrchr($fileName, '.'));
         if (strstr('*.jpg;*.jpeg;*.gif;*.png', $extensao)) {
             $newName = $imgName ? $imgName : str_replace($extensao, '', $fileName);
             $newName = Strings::stringToUrl($newName);
             $newName = strtolower(substr($newName, 0, $limitName - 8) . $extensao);
             $newName = NwdGD::autoRename($pastaDestino, $newName);
             if (!is_dir('_temp')) {
                 mkdir('_temp', 0755);
                 sleep(1);
             }
             $imageOutput = $pastaDestino . $newName;
             $imageResize = NwdGD::imgResize($file, $imageOutput, $largImg, $altImg, $imgProporc, $corBg, $imgQualidade, $imgMenor, $watermark);
             if ($geraThumb) {
                 if (!is_dir($pastaDestino . 'thumbs')) {
                     mkdir($pastaDestino . 'thumbs', 0755);
                 }
                 $thumbOutput = $pastaDestino . 'thumbs/' . $newName;
                 $thumbResize = NwdGD::imgResize($file, $thumbOutput, $largThumb, $altThumb, $thumbProporc, $corBg, $imgQualidade, $imgMenor);
             }
             if ($imageResize) {
                 $result = $newName;
             }
         }
     }
     return $result;
 }