PHP Fotoğraf Boyutlandırma

UgurOnline

Asistan
Katılım
11 Aralık 2008
Mesajlar
159
Reaksiyon puanı
1
Puanları
0
sunucuya yüklediğimiz bir fotoğrafın boyutunu 1024x768'e nasıl orantılayabilirim? veya orijinal şekilde upload edilsin ama <img ile gösterdiğimde orantılı şekilde göstersin. bunu nasıl yapabilirim?

teşekkürler
 

sdelta

Profesör
Katılım
23 Nisan 2008
Mesajlar
4,234
Reaksiyon puanı
145
Puanları
243
Kod:
<?php 
/* 
* File: SimpleImage.php 
* Author: Simon Jarvis 
* Copyright: 2006 Simon Jarvis 
* Date: 08/11/06 
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php 
*  
* This program is free software; you can redistribute it and/or  
* modify it under the terms of the GNU General Public License  
* as published by the Free Software Foundation; either version 2  
* of the License, or (at your option) any later version. 
*  
* This program is distributed in the hope that it will be useful,  
* but WITHOUT ANY WARRANTY; without even the implied warranty of  
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  
* GNU General Public License for more details:  
* http://www.gnu.org/licenses/gpl.html 
* 
*/ 
  
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;    
   }       
} 
?>
Save the above file as SimpleImage.php and take a look at the following examples of how to use the script.

The first example below will load a file named picture.jpg resize it to 250 pixels wide and 400 pixels high and resave it as picture2.jpg
Kod:
<?php 
   include('SimpleImage.php'); 
   $image = new SimpleImage(); 
   $image->load('picture.jpg'); 
   $image->resize(250,400); 
   $image->save('picture2.jpg'); 
?>
If you want to resize to a specifed width but keep the dimensions ratio the same then the script can work out the required height for you, just use the resizeToWidth function.
Kod:
<?php 
   include('SimpleImage.php'); 
   $image = new SimpleImage(); 
   $image->load('picture.jpg'); 
   $image->resizeToWidth(250); 
   $image->save('picture2.jpg'); 
?>
You may wish to scale an image to a specified percentage like the following which will resize the image to 50% of its original width and height
Kod:
<?php 
   include('SimpleImage.php'); 
   $image = new SimpleImage(); 
   $image->load('picture.jpg'); 
   $image->scale(50); 
   $image->save('picture2.jpg'); 
?>
You can of course do more than one thing at once. The following example will create two new images with heights of 200 pixels and 500 pixels
..........
http://forum.iyinet.com/php/129713-php-resim-boyutlandirma-ve-kayit.html
 

UgurOnline

Asistan
Katılım
11 Aralık 2008
Mesajlar
159
Reaksiyon puanı
1
Puanları
0
teşekkür ederim

---------- Post added at 17:24 ---------- Previous post was at 15:28 ----------

aslında benim istediğim tam bu değildi

mesela site üzerinde 400x500 ebatlarında resim gösteriyorum. resim yolunu veritabanından çekiyorum (ebatlar sabit). sunucuya yüklenen resimler örneğin 200x600 ebatlarındaysa resim düzgün görünmüyor. en küçülecekse boyda ona orantılı şekilde küçülecek. c#'taki fixed gibi

---------- Post added at 18:42 ---------- Previous post was at 17:24 ----------

şöyle bir fonksiyon buldum ama ufak bi problem var

Kod:
<?php
function thumbnail_box($img, $box_w, $box_h) {
    //create the image, of the required size
    $new = imagecreatetruecolor($box_w, $box_h);
    if($new === false) {
        //creation failed -- probably not enough memory
        return null;
    }


    //Fill the image with a light grey color
    //(this will be visible in the padding around the image,
    //if the aspect ratios of the image and the thumbnail do not match)
    //Replace this with any color you want, or comment it out for black.
    //I used grey for testing =)
    $fill = imagecolorallocate($new, 200, 200, 205);
    imagefill($new, 0, 0, $fill);

    //compute resize ratio
    $hratio = $box_h / imagesy($img);
    $wratio = $box_w / imagesx($img);
    $ratio = min($hratio, $wratio);

    //if the source is smaller than the thumbnail size, 
    //don't resize -- add a margin instead
    //(that is, dont magnify images)
    if($ratio > 1.0)
        $ratio = 1.0;

    //compute sizes
    $sy = floor(imagesy($img) * $ratio);
    $sx = floor(imagesx($img) * $ratio);

    //compute margins
    //Using these margins centers the image in the thumbnail.
    //If you always want the image to the top left, 
    //set both of these to 0
    $m_y = floor(($box_h - $sy) / 2);
    $m_x = floor(($box_w - $sx) / 2);

    //Copy the image data, and resample
    //
    //If you want a fast and ugly thumbnail,
    //replace imagecopyresampled with imagecopyresized
    if(!imagecopyresampled($new, $img,
        $m_x, $m_y, //dest x, y (margins)
        0, 0, //src x, y (0,0 means top left)
        $sx, $sy,//dest w, h (resample to this size (computed above)
        imagesx($img), imagesy($img)) //src w, h (the full size of the original)
    ) {
        //copy failed
        imagedestroy($new);
        return null;
    }
    //copy successful
    return $new;
}
?>

Kod:
$i = imagecreatefromjpeg("fotograf.jpg");
$thumb = thumbnail_box($i, 150, 110);

imagedestroy($i);

if(is_null($thumb)) {
    /* image creation or copying failed */
    header('HTTP/1.1 500 Internal Server Error');
    exit();
}

header('Content-Type: image/jpeg');
imagejpeg($thumb);

ben bu fotoğraftan yanyana 2 tane gösterilmesini istiyorum. bunu yapsam yeticek:huh:
 

mstfcck

Asistan
Katılım
18 Mart 2010
Mesajlar
174
Reaksiyon puanı
5
Puanları
0
PHP:
<?php
$foto = "demo.jpg";
$size = getimagesize("$foto");
$imgH = $size[1];
$imgW = $size[0];
$oran = $imgH / $imgW;
$sabit = 400;
if ($imgW > $sabit) 
{
$oran = $imgW / $imgH;
$imgW = $sabit;
$imgH = $sabit / $oran;
}
echo "<img border='0' src='$foto' width='$imgW' height='$imgH'/>";
?>

Bu kodda işine yarayabilir. Daha önce javascript ile yapmıştım. Şimdi de php ile yaptım. Resmin genişliğini 400 olarak belirledim ben. Bunu istediğiniz bütün resimlere uygulayabilirsiniz.
 
Üst