php裁剪图片没有上传它

时间:2011-03-14 03:51:40

标签: php upload photo crop

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

我希望在使用此代码的同时从数据库中查看我的照片。

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

谢谢

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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
}
?>

检查出来......

答案 2 :(得分: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);