【问题标题】:How to put Image into directory imagejpeg()? [closed]如何将图像放入目录 imagejpeg()? [关闭]
【发布时间】:2013-03-11 16:43:35
【问题描述】:

好的,我有这段代码可以裁剪图像并显示它,我想做的不仅仅是显示它,我想将图像保存在一个文件夹中,这里是完整代码

    <?php
/*
 * Crop-to-fit PHP-GD
 * http://salman-w.blogspot.com/2009/04/crop-to-fit-image-using-aspphp.html
 *
 * Resize and center crop an arbitrary size image to fixed width and height
 * e.g. convert a large portrait/landscape image to a small square thumbnail
 */

define('DESIRED_IMAGE_WIDTH', 512);
define('DESIRED_IMAGE_HEIGHT', 289);

$source_path = $_FILES['Image1']['tmp_name'];

/*
 * Add file validation code here
 */

list($source_width, $source_height, $source_type) = getimagesize($source_path);

switch ($source_type) {
    case IMAGETYPE_GIF:
        $source_gdim = imagecreatefromgif($source_path);
        break;
    case IMAGETYPE_JPEG:
        $source_gdim = imagecreatefromjpeg($source_path);
        break;
    case IMAGETYPE_PNG:
        $source_gdim = imagecreatefrompng($source_path);
        break;
}

$source_aspect_ratio = $source_width / $source_height;
$desired_aspect_ratio = DESIRED_IMAGE_WIDTH / DESIRED_IMAGE_HEIGHT;

if ($source_aspect_ratio > $desired_aspect_ratio) {
    /*
     * Triggered when source image is wider
     */
    $temp_height = DESIRED_IMAGE_HEIGHT;
    $temp_width = ( int ) (DESIRED_IMAGE_HEIGHT * $source_aspect_ratio);
} else {
    /*
     * Triggered otherwise (i.e. source image is similar or taller)
     */
    $temp_width = DESIRED_IMAGE_WIDTH;
    $temp_height = ( int ) (DESIRED_IMAGE_WIDTH / $source_aspect_ratio);
}

/*
 * Resize the image into a temporary GD image
 */

$temp_gdim = imagecreatetruecolor($temp_width, $temp_height);
imagecopyresampled(
    $temp_gdim,
    $source_gdim,
    0, 0,
    0, 0,
    $temp_width, $temp_height,
    $source_width, $source_height
);

/*
 * Copy cropped region from temporary image into the desired GD image
 */

$x0 = ($temp_width - DESIRED_IMAGE_WIDTH) / 2;
$y0 = ($temp_height - DESIRED_IMAGE_HEIGHT) / 2;
$desired_gdim = imagecreatetruecolor(DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT);
imagecopy(
    $desired_gdim,
    $temp_gdim,
    0, 0,
    $x0, $y0,
    DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT
);

/*
 * Render the image
 * Alternatively, you can save the image in file-system or database
 */
header('Content-type: image/jpeg');
imagejpeg( $desired_gdim );
/*
 * Add clean-up code here
 */
?>

我的问题是在最后一行imagejpeg( $desired_gdim ); 应该是imagejpeg( $desired_gdim, 'img/whatever-filename.jpg' );

但是图像甚至没有显示在浏览器中,也没有发送到目录,如果我想用我已经上传的文件名插入它,同样的事情会发生只是一个破碎图像的小图标

【问题讨论】:

  • header(content-type : image/jpeg), 你想显示图片吗,保存就用imagejpeg ( $desired_gdim $filename )
  • 定义“不工作”。发生了什么或没有发生什么?你有任何错误吗?你检查了吗?
  • remove header(content-type),用于渲染不保存。问候
  • 它不工作是什么意思?您是否收到任何类型的错误消息?
  • “它不起作用” 不是一个很好的问题。这需要首先进行基本调试。请在脚本的开头添加error_reporting(~0); ini_set('display_errors', 1);。此外,您应该enable error logging 并按照错误日志进行操作。

标签: php html


【解决方案1】:

您必须在两者之间进行选择 - 显示图像或将其保存到目录。你不能两者都做。

imagejpeg($gd_object); //shows the image, requires content-type to be image/jpeg

imagejpeg($gd_object, $filepath); //saves the image, to $filepath

如果你想显示它,你可以尝试保存图像,然后将使用重定向到图像。

imagejpeg($gd_object, $filepath);
header("Location: $filepath");

【讨论】:

    【解决方案2】:

    将您的路径 'img/whatever-filename.jpg' 更改为您可以使用 $_SERVER['DOCUMENT_ROOT'] 获取的文档根目录并附加您想要的路径。例如 $_SERVER['DOCUMENT_ROOT'].'/img/whatever-filename.jpg';

    【讨论】:

      猜你喜欢
      • 2016-02-08
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多