2011-03-14 46 views
2
//Your Image 
$imgSrc = "image.jpg"; 

list($width, $height) = getimagesize($imgSrc); 

$myImage = imagecreatefromjpeg($imgSrc); 

if ($width > $height) { 
    $y = 0; 
    $x = ($width - $height)/2; 
    $smallestSide = $height; 
} else { 
    $x = 0; 
    $y = ($height - $width)/2; 
    $smallestSide = $width; 
} 

$thumbSize = 100; 
$thumb = imagecreatetruecolor($thumbSize, $thumbSize); 
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide); 

//Output 
imagejpeg($thumb); 

頁評論:PHP裁剪圖像,而不把它上傳

<?php 

$sql1 = mysql_query("SELECT * FROM images"); 
while($row_img = mysql_fetch_array($sql1)){ 
$img_url=$row_img['3']; ?> 
     <img src="/<? echo $img_url; ?>" />   
<?php } ?> 

我想利用這個代碼,同時查看從數據庫中我的照片。

我的圖像名稱爲db $imgSrc=$img_url;即行名。

謝謝

+2

好像有很多的代碼從此卻下落不明。通過方法名稱判斷,它聽起來像是在創建圖像,而不是從數據庫中檢索它。你可能想澄清你正在嘗試做什麼。你的問題越好,答案越好:) – jmort253

+0

現在看看代碼,我編輯它:) – rixlinux

+0

所以你想:1從數據庫檢索圖像文件名? 2.裁剪? 3.顯示裁剪的圖像? –

回答

0

這將工作

$url = "http://localhost/sample.jpg"; 

$src = file_get_contents($url); 

$myImage = imagecreatefromstring($src); 


if ($width > $height) { 
$y = 0; 
$x = ($width - $height)/2; 
$smallestSide = $height; 
} else { 
$x = 0; 
$y = ($height - $width)/2; 
$smallestSide = $width; 
} 

$thumbSize = 100; 

$thumb = imagecreatetruecolor($thumbSize, $thumbSize); 

imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide); 

header('Content-Type: image/jpeg'); 
imagejpeg($myImage); 
0
$url = "http://www.huddletogether.com/projects/lightbox2/images/image-2.jpg"; 

$src = file_get_contents($url); 

$filename = end(explode('/', $url)); 

$handle = fopen($filename, 'w'); 

fwrite($handle, $src); 

fclose($handle); 

$myImage = imagecreatefromjpeg($filename); 

//do your other stuff here 
0

創建一個文件名resizer.php和下面的代碼記下該文件(resizer.php)

 


class Resizer { 

    public $image_to_resize; 
    public $new_width; 
    public $new_height; 
    public $ratio; 

    public function resize() { 

     if(!file_exists($this->image_to_resize)) { 
      exit("File ".$this->image_to_resize." does not exist."); 
     } 

     $info = getimagesize($this->image_to_resize); 

     if(empty($info)) { 
      exit("The file ".$this->image_to_resize." doesn't seem to be an image."); 
     } 

     $width = $info[0]; 
     $height = $info[1]; 
     $mime = $info['mime']; 

     if($this->ratio) { 
      if (isset($this->new_width)) { 
       $factor = (float)$this->new_width/(float)$width; 
       $this->new_height = $factor * $height; 
      } 
      else if (isset($this->new_height)) { 
       $factor = (float)$this->new_height/(float)$height; 
       $this->new_width = $factor * $width; 
      } 
      else exit("neither new height or new width has been set"); 
     } 

     $type = substr(strrchr($mime, '/'), 1); 

     switch (strtolower($type)) { 
      case 'jpeg': 
       $image_create_func = 'imagecreatefromjpeg'; 
       $image_save_func = 'imagejpeg'; 
       $new_image_ext = 'jpg'; 
       break; 

      case 'png': 
       $image_create_func = 'imagecreatefrompng'; 
       $image_save_func = 'imagepng'; 
       $new_image_ext = 'png'; 
       break; 

      case 'bmp': 
       $image_create_func = 'imagecreatefrombmp'; 
       $image_save_func = 'imagebmp'; 
       $new_image_ext = 'bmp'; 
       break; 

      case 'gif': 
       $image_create_func = 'imagecreatefromgif'; 
       $image_save_func = 'imagegif'; 
       $new_image_ext = 'gif'; 
       break; 

      case 'vnd.wap.wbmp': 
       $image_create_func = 'imagecreatefromwbmp'; 
       $image_save_func = 'imagewbmp'; 
       $new_image_ext = 'bmp'; 
       break; 

      case 'xbm': 
       $image_create_func = 'imagecreatefromxbm'; 
       $image_save_func = 'imagexbm'; 
       $new_image_ext = 'xbm'; 
       break; 

      default: 
       $image_create_func = 'imagecreatefromjpeg'; 
       $image_save_func = 'imagejpeg'; 
       $new_image_ext = 'jpg'; 
     } 

     // New Image 
     $image_c = imagecreatetruecolor($this->new_width, $this->new_height); 

     $new_image = $image_create_func($this->image_to_resize); 

     imagecopyresampled($image_c, $new_image, 0, 0, 0, 0, $this->new_width, $this->new_height, $width, $height); 

    header("Content-Type: ".$mime); 
    $image_save_func($image_c); 
     $process = $image_save_func($image_c); 

    } 
} 

// Default Width and Height 
$def_width = 100; 
$def_height = 100; 

// Get Variables from Get 
$image_file_path = $_GET['image_file']; 
$width = ((isset($_GET['width']) && intval($_GET['width'])!=0) ? $_GET['width'] : $def_width); 
$height = ((isset($_GET['height']) && intval($_GET['height'])!=0) ? $_GET['height'] : $def_height); 

// Use Resizer Class 
$image = new Resizer(); 

// Set New Cropped Dimensions 
$image->new_width = $width; 
$image->new_height = $height; 

// Path of Image to Resize 
$image->image_to_resize = $image_file_path; 

// Maintains Image Ratio 
$image->ratio = true; 

$image->resize(); 


 

resizer.php文件的使用

 

<?php 

$sql1 = mysql_query("SELECT * FROM images"); 

while($row_img = mysql_fetch_array($sql1)) { 
    $img_url = $row_img['3']; 
?> 
    <img src="resizer.php?image_file=<?php echo $img_url; ?>&width={NEW_WIDTH_FOR_IMAGE}&height={NEW_HEIGHT_FOR_IMAGE}" /> 
<?php 
} 
?> 

檢查出來...