Resize poza la dimensiune fixa

Discutii despre script-uri si coduri PHP-MySQL, precum si lucru cu XML in PHP.
cadou
Mesaje: 328

Resize poza la dimensiune fixa

Folosesc acest cod de incarcare dar doresc sa si redimensioneze pozele din marimea lor naturala sa le redimensioneze in 300 pe 300 . Nu sa decupeze sa le redimensioneze !

Cod: Selectaţi tot

<?php 
include('conectare.php');
        $uploadpath = 'poza-profil/'; // directory to store the uploaded files
        $max_size = 10000; // maximum file size, in KiloBytes
        $alwidth = 10000; // maximum allowed width, in pixels
        $alheight = 10000; // maximum allowed height, in pixels
        $allowtype = array('jpg', 'jpe'); // allowed extensions

        if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1) {
        $sepext = explode('.', strtolower($_FILES['fileup']['name']));
        $type = end($sepext); // gets extension

        $nume = strip_tags($_POST['nume']);       // nume_utilizator
        $id = 0;      // valoare initiala, in caz ca nu e gasit vreun "id"
       
$sql = mysql_query("SELECT `id` FROM `membri` WHERE `nume` = '$nume' LIMIT 1");
$sql = mysql_fetch_assoc($sql);
if(!empty($sql['id']))
{
         $id = $sql['id'];
}

        $uploadpath = $uploadpath . $id. '.'. $type; // gets the file name

        list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']); // gets image width and height
        $err = ''; // to store the errors


        // Checks if the file has allowed type, size, width and height (for images)
        if(!in_array($type, $allowtype)) $err .= '<div id=eroare>Poza: <b>'. $_FILES['fileup']['name']. '</b> nu are tipul de extensie acceptata - jpg sau jpe.</div>';
        if($_FILES['fileup']['size'] > $max_size*1000) $err .= '<div id=eroare>Marimea maxima trebuie sa fie de: '. $max_size. ' KB.</div>';
        if(isset($width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) $err .= '<div id=eroare>Marimea maxima a inaltimii si latimii trebuie sa fie: '. $alwidth. ' x </div>'. $alheight;

        // If no errors, upload the image, else, output the errors
        if($err == '') {
        if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) {
        echo '';
        }
        else echo '<div id=eroare>Poza nu a putut fi incarcata. Incearca alta poza.</div>';
        }
        else echo $err;
        }
?>

MarPlo Mesaje: 4343
Pentru rezize imagine se poate folosi functia imagecopyresized().
Este un exemplu cu aceasta functie la pagina: https://marplo.net/php-mysql/functii_imagini.html
Sau in manualul php, la adresa:
php.net/manual/en/function.imagecopyresized.php

Dupa ce se face upload la poza, faci un script care preia poza de unde e salvata, si o re-salveaza redimensionata cu acelasi nume.

evident Mesaje: 168
redimensionare.php

Cod: Selectaţi tot

<?php 
class SimpleImage {
   var $image;
   var $image_type;
 
   function load($filename) {
      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
 
         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
 
         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
 
         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
 
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {
 
         imagegif($this->image,$filename);
      } elseif( $image_type == IMAGETYPE_PNG ) {
 
         imagepng($this->image,$filename);
      }
      if( $permissions != null) {
 
         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {
 
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {
 
         imagegif($this->image);
      } elseif( $image_type == IMAGETYPE_PNG ) {
 
         imagepng($this->image);
      }
   }
   function getWidth() {
 
      return imagesx($this->image);
   }
   function getHeight() {
 
      return imagesy($this->image);
   }
   function resizeToHeight($height) {
 
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }
 
   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }
 
   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100;
      $this->resize($width,$height);
   }
 
   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   }      
 
}
?>
index.php

Cod: Selectaţi tot

include('redimensionare.php');
   $image = new SimpleImage();
   $image->load ("poza.jpg");
   $image->resize(400,250);
   $image->save("poza.jpg");
 

cadou Mesaje: 328
tot nu am reusit, pana la urma cred ca ma las pagubas si voi face redimensionarea pozelor pe un alt site specialt pt asa ceva