【问题标题】:Create image with PHP and store it in a server folder使用 PHP 创建图像并将其存储在服务器文件夹中
【发布时间】:2017-02-20 03:56:55
【问题描述】:

解决了问题!!!

    1234563 > 为了获得高质量的图片
  1. 图像文件夹没有斜线“images/fb/”

  2. 对于imagecolorallocate imagecolorallocate($im, 142, 011, 029) 三位数RGB 也可以得到正确的输出。

所以,下面给出了这个问题的答案和完整的代码

<?php
// Create image with 2 line of text
$im = imagecreatetruecolor(600, 300);
$text_color = imagecolorallocate($im, 142, 011, 029);
imagestring($im, 10, 5, 5,  'Some Text String', $text_color);
imagestring($im, 10, 5, 50,  'Some Text String 2', $text_color);

// Set the content type header as image/jpeg
header('Content-Type: image/jpeg');

// Output the image
//imagejpeg($im);    //up-to this point its working well 

$name="test2";
$save = "images/fb/". strtolower($name) .".jpeg";
imagejpeg($im, $save, 100);   //Saves the image
imagejpeg($im);              //Displays the image

// Free up memory
imagedestroy($im);
?>

问题

请帮我解决这个问题,

我正在尝试通过 php 创建图像并尝试将创建的图像存储在文件夹中,图像确实显示了,但是当我将保存添加到文件夹时它给了我错误。以下是我尝试过的代码。

<?php
// Create image with 2 line of text
$im = imagecreatetruecolor(600, 300);
$text_color = imagecolorallocate($im, 142, 11, 29);
imagestring($im, 10, 5, 5,  'Some Text String', $text_color);
imagestring($im, 10, 5, 50,  'Some Text String 2', $text_color);

// Set the content type header as image/jpeg
header('Content-Type: image/jpeg');

// Output the image
//imagejpeg($im);    //up-to this point its working well 

$name="test";
$save = "/images/fb/". strtolower($name) .".jpeg";

imagejpeg($im, $save, 0, NULL);

// Free up memory
imagedestroy($im);
?>

以下是我遇到的错误

The image "path" cannot be displayed because it contains errors.

我的文件夹结构是

->www
  -> projectFolder 
   -> subFolder
      ->phpFile
      ->imageFolder (images)
        ->imageSubFolder (fb)

我也尝试过chmod,但它的权限似乎不是问题,因为我正在 Localhost 中尝试这个。

任何建议将不胜感激......

【问题讨论】:

  • 您确定路径/images/fb/ 存在吗? / 表示这是服务器上的主文件夹,而不是您本地的 images/fb/ 文件夹
  • 也许你的路径错了?什么错误?
  • "...它给了我错误" ...错误是什么?
  • $save = "/path/to/www/images/fb/". strtolower($name) .".jpeg";您需要使用完整的服务器路径,或者使用相对路径并省略图像前的斜线$save = "images/fb/". strtolower($name) .".jpeg";
  • 我确实通过省略图像前面的 / 来尝试使用相对路径,但这没有帮助。

标签: php imagejpeg


【解决方案1】:

您将图像创建为$im,然后尝试将其保存为.$my_img

改变这个:imagejpeg($my_img, $save, 0);

收件人:imagejpeg($im, $save, 0);

【讨论】:

  • 是的,这是一个错误,“已更正”虽然仍然是同样的问题 :(
  • 澄清一下,根据 PHP 手册,Null 也可以用作imagejpeg 的参数。如果我没有为图像设置任何质量。
  • imagejpeg 只有 3 个参数。如果您想设置质量并且不想提供 $to 参数,您可以为 $to 参数使用 NULL:php.net/manual/en/function.imagejpeg.php
  • 谢谢moni_dragu,很少有人认为需要随着您的建议而改变。谢谢你指导我!
猜你喜欢
  • 1970-01-01
  • 2020-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多