如何使用 PHP 打印功能打印图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18592769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:52:34  来源:igfitidea点击:

How To Print Images with PHP print function

phpjavascripthtmlcss

提问by dagogodboss

Please help me out. I have a form that collects picture from an upload field in a client form and the picture to be printed out on the user page. this work perfectly well on localhost and it looks like the below Ilamini Ayebatonye Thanks A lot

请帮帮我。我有一个表单,它从客户端表单的上传字段中收集图片,并将图片打印在用户页面上。这在本地主机上工作得很好,它看起来像下面的 Ilamini Ayebatonye 非常感谢

NAME:     Name Surname

EMAIL ADDRESS:     [email protected]

HOME ADDRESS:     123 Random Street, FakeTown

PHONE NUMBER:     012345678911

QUALIFICATION:     I.C.T Certificate

SECTOR:     CCNA

INTEREST:     please put in your interest

Please Print this slip, attached your C.V and photocopies of your credentials and come with it to the day of the seminar.

请打印此单据,附上您的简历和证书复印件,并在研讨会当天随身携带。

but the problem with is that when I uploaded the site and try registering the picture won't show again,

但问题是当我上传网站并尝试注册时,图片不会再次显示,

what i see is the below

我看到的是下面

ECO9JA-CREATING JOBS IN ICT
          Backgrounds_15670_zps11bc080d.png    

NAME:     Name Surname

EMAIL ADDRESS:     [email protected]

HOME ADDRESS:     123 Random Street, FakeTown

PHONE NUMBER:     012345678911

QUALIFICATION:     I.C.T Certificate

SECTOR:     CCNA

INTEREST:     please put in your interest

Please Print this slip, attached your C.V and photocopies of your credentials and come with it to the day of the seminar.

请打印此单据,附上您的简历和证书复印件,并在研讨会当天随身携带。

THANKS FOR REGISTERING

感谢注册

the script is below for the image variable the form upload and the print fxn are as follows:

表单上传和打印 fxn 的图像变量的脚本如下:

VARIABLE

多变的

<?php
$file= "file.csv";
$name = $_POST['name'];
$email = $_POST['email'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$qualification = $_POST['qualification'];
$sector = $_POST['sector'];
$interest = $_POST['interest'];
$image= $_POST['image'];
<input type="hidden" name="MAX_FILE_SIZE" value="500">
<P>
<br>
<input type="file" name="image" value="upload" >

PRINT THIS CODE ON USERPAGE

在用户页面上打印此代码

<?php print"<img src=\"$image\" width=\"100px\" height=\"100px\"\/>";?>

回答by Aklesh Singh

Use this just copy and paste in your project.

使用它只需复制并粘贴到您的项目中。

$contents=  file_get_contents('http://localhost/elgg_social/data/36/641483447202.jpg');

$expires = 14 * 60*60*24;

header("Content-Type: image/jpeg");
header("Content-Length: " . strlen($contents));
header("Cache-Control: public", true);
header("Pragma: public", true);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT', true);

echo $contents;
exit;

回答by mickzer

The problem must lie when you are posting the path for the image file. I have tested the following php with an image "test.png" and it works.

当您发布图像文件的路径时,问题一定出在。我已经使用图像“test.png”测试了以下 php 并且它有效。

<?php 
$image="test.png";
print"<img src=\"$image\" width=\"100px\" height=\"100px\"\/>";
?>

回答by Mubashir

Looks like there is problem with your image attributes, remove "px". it should be like this

看起来您的图像属性有问题,请删除“px”。应该是这样的

<?php print"<img src=\"$image\" width=\"100\" height=\"100\"\/>";?>

回答by Vignesh Pichamani

Based on Uploading your photo

基于上传你的照片

<input type="file" name="image" value="upload" >

It fetches the file path itself. you could move the uploaded file to your folder and the corresponding file path is to save in db.

它获取文件路径本身。您可以将上传的文件移动到您的文件夹中,相应的文件路径保存在db中。

If you are uploading the path as looks like

如果你上传的路径看起来像

C:\Users\Public\Pictures\Sample Pictures\Desert.jpg

If you are posted as $image = $post['image']

如果您被发布为 $image = $post['image']

Echo the path it returns to error or null

回显它返回错误或空的路径

<img src="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"/>

If sometimes works but most time won't works as i feel this type of problem.

如果有时有效但大多数时间不起作用,因为我觉得这种问题。

Thanks.

谢谢。

回答by syd619

Based on the reference I commented on your php script you should have a code similar to this:

根据我对你的 php 脚本的评论,你应该有一个类似于这样的代码:

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }

Take a close look line by line. You can set the maximum file size for your image upload at: $_FILES["file"]["size"] < 20000

一行一行地仔细观察。您可以在以下位置设置图像上传的最大文件大小:$_FILES["file"]["size"] < 20000

Furthermore this will code will make sure that the user is uploading an image and not something else.

此外,这将代码将确保用户正在上传图像而不是其他内容。

Finally based on your need and this script after successful upload

最后根据你的需要和成功上传后的这个脚本

 move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; 

Then you could do this to show the image:

然后你可以这样做来显示图像:

echo '<img src="upload/'.$_FILES["file"]["name"].'">';

回答by Siddharth Shukla

  header('Content-Type: image/jpeg');
  print $response;