【问题标题】:Error displaying image in pdf directory - Cakephp 3在 pdf 目录中显示图像时出错 - Cakephp 3
【发布时间】:2020-11-11 03:54:17
【问题描述】:

朋友们,我设置了一个购物车,其中包含一个可以插入多种产品的购物车。除了购物车,我还需要以 pdf 格式导出此列表。生成pdf时,除了产品图片,所有与产品相关的项目都会显示出来。

我在存储图像路径的表中有两个字段:pathimg_product

我有一个填充字段的示例:

path: img/img_products/bd9399cfadbbb51639a033a6a81208bd04cf4b85
img_product: cell.jpg

我正在尝试以这种方式生成图像: 订单/pdf/download.ctp

<?php foreach((array) $this->request->getSession()->read('cart') as $index=>$cart): ?>

<?= $cart->has('product') ? $this->Html->image('../'.$cart->product->path.$cart->product->img_product, array('width' => '150px', 'class' => 'card-img-top')) : ''?>

<?= $cart->product->name ?>

<?php echo number_format($cart->product->price, 2, ',', '') ?></p>

Quantity: <?= $cart->quantity ?>
 
Subtotal: R$ <?php $sub = ($cart>product->price * $cart->quantity);
             echo number_format($sub, 2, ',', '');
             ?>

<?php endforeach; ?>

也就是说,这行不行:

<?= $cart->has('product') ? $this->Html->image('../'.$cart->product->path.$cart->product->img_product, array('width' => '150px', 'class' => 'card-img-top')) : ''?>

我的文件夹位于:../webroot/img/img_products

我感谢任何 cmets

【问题讨论】:

  • 这张图片生成的 HTML 中会显示什么 URL?您是否有充分的理由使用“../”来启动它而不仅仅是“/”?
  • 我只留下'/',图像没有显示。当我选择产品时,图像在购物车中正确显示,因为它在代码中,但是当我复制代码以进行 download.ctp 时,它不再出现。
  • project.exemple.net/img/img_products/… -> 这是我放入购物车时生成的url。
  • 它有 40 个字符的 GUID,然后是“tinta.jpg”,中间没有斜线,这对吗?
  • 它工作的页面的完整 URL 是什么,该页面上 img 标记的 HTML 是什么,它不工作的页面的 URL 是什么?跨度>

标签: php session cakephp cakephp-3.0


【解决方案1】:

请参阅 CakeHelper 的 HTML API:

https://api.cakephp.org/3.0/class-Cake.View.Helper.HtmlHelper.html#_image

echo $this->Html->image('cake_icon.png', ['alt' => 'CakePHP']);

此图像路径将是 your_application_webroot_folder/img/cake_icon.png

所以,根据您的问题和评论,我猜路径是:

your_application_webroot_folder/img/img_products/some_unique_number/image_name.extention

HTML 链接将用于此

echo $this->Html->image('/img_products/some_unique_number/image_name.extention');

您还提到一个字段 [name= path] 包含从 webroot 到图像目录 的完整路径,而另一个字段 [name= img_product ] 包含文件名。所以我认为你不需要 $this->Html->image。您在数据库中保存了完整的图像路径。

你可以使用

$this->request->webroot.$cart->product->path.$cart->product->img_product

或者你可以使用(如果以上剂量不起作用)

\Cake\Routing\Router::url('/'.$cart->product->path.$cart->product->img_product,true);

详情:https://api.cakephp.org/3.4/class-Cake.Routing.Router.html#_url

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-13
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    相关资源
    最近更新 更多