2012-09-14 81 views
0

再一次,我在這裏遇到了一個簡單的問題,我無法解決,因爲我使用的搜索術語顯然與以前使用的搜索術語不匹配。我想創建的只是一小段腳本,可以使用URL更改圖像寬度。通過URL更改圖片大小

一個例子可以在http://waarneming.nl/fotonew/0/2639780.jpg上找到。如果添加變量?w=100&h=100,圖像將調整大小。值得一提的是,我不需要裁剪(雖然100x100當然不是正確的比例,但在示例中也不會發生這種情況)。唯一發生的事情是可以用CSS和HTML做什麼:更改更大圖片的高度和寬度。但是因爲我需要在另一個腳本中調整大小的圖像,所以我想使用URL變量。

希望你能幫助!

+0

你想保存這些影像尺寸(即保存圖像與它的新的大小)或只是在不同大小顯示圖像? – legrandviking

+0

只需顯示它。它不應該創建一個新文件。 – Stijn

回答

6

使用SimpleImage庫:

<?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; 
    }  

} 
?> 

那麼,試試這個:

<?php 
    include('SimpleImage.php'); 
    $image = new SimpleImage(); 
    $image->load($_GET['file']); 
    $image->resize($_GET['width'], $_GET['height']); 
    $image->output(); 
?> 

爲了調用腳本,你會傳遞文件名,通過你的URL的寬度和高度。假設腳本名爲foo.php:

http://www.example.com/foo.php?file=picture_name.jpg&width=100&height=100

+0

如果你想做服務端,爲什麼不使用GD? – boruch

+0

這只是GD上的一個抽象,用於簡化使用 –

0

您可以在JavaScript中獲取參數並將其分配給圖像。 喜歡的東西:

function gup(name) 
{ 
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
    var regexS = "[\\?&]"+name+"=([^&#]*)"; 
    var regex = new RegExp(regexS); 
    var results = regex.exec(window.location.href); 
    if(results == null) 
    return ""; 
    else 
    return results[1]; 
} 

然後:

var imagelengh = gup('w') 
$('#image').width(w)