php裁剪图像而不上传它

发布于 2024-10-21 15:24:56 字数 905 浏览 12 评论 0原文

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

我想使用此代码来查看数据库中的照片。

我在数据库 $imgSrc=$img_url; 中的图像名称是行名称。

谢谢

//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);

PAGE REVIEW :

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

I want to use this code with while to view my photo from database.

my image name in db $imgSrc=$img_url; that is row name.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼泪都笑了 2024-10-28 15:24:56

这会起作用

$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);

This will work

$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);
空名 2024-10-28 15:24:56
$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
$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
小镇女孩 2024-10-28 15:24:56

创建一个名为 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
}
?>

检查一下...

create a file names resizer.php and write down the codes below to that file(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 file usage

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

check it out...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文