php обрезать изображение без загрузки - PullRequest
2 голосов
/ 14 марта 2011
//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

$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;, то есть в строкеимя.

Спасибо

Ответы [ 3 ]

0 голосов
/ 14 марта 2011

создайте файл с именами 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; ?&gt&width={NEW_WIDTH_FOR_IMAGE}&height={NEW_HEIGHT_FOR_IMAGE}" />
<?php
}
?>

проверить это ...

0 голосов
/ 14 марта 2011

Это будет работать

$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 голосов
/ 14 марта 2011
$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
...