dongyang4615 2014-12-11 13:12
浏览 44
已采纳

Codeigniter - FIles必须位于同一文件夹中

I'm currently using imagecreatefromjpeg() to add text to an image. I have it working with all my files are in one folder, however I'm using CodeIgniter so my files are distributed throughout different folders.

What's the workaround to accomplish this?

I was thinking to put all my required files in my views, however that wasn't working.

See error

A PHP Error was encountered

Severity: Warning

Message: imagettftext(): Could not find/open font

Filename: views/coupon.php

Line Number: 30

See code

<?php
//Report any errors
ini_set("display_errors", "1");
error_reporting(E_ALL);
//Set the Content Type
      // header('Content-type: image/jpeg');

      // Create Image From Existing File
      $jpg_image = imagecreatefromjpeg('http://localhost:8888/game/rcw/assets/couponWrite.jpg');


      // Allocate A Color For The Text
      $white = imagecolorallocate($jpg_image, 155, 155, 155);

      // Set Path to Font File
      $font_path = 'http://localhost:8888/game/rcw/application/views/Arial.ttf';

      // Set Text to Be Printed On Image
      $text = "IGL" . rand(55555,99999);

      // Print Text On Image
      imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);

      // Send Image to Browser
      imagejpeg($jpg_image);

      // Clear Memory
      imagedestroy($jpg_image);


 ?>
  • 写回答

1条回答 默认 最新

  • doujuan2688 2014-12-11 13:41
    关注

    All you have to do is to access the image file using its path, either relative to the path of the PHP file that processes it, or better, its absolute path. For example, let's say your code is in a controller, in file application/controllers/myImageProcessor.php and you want to load a file stored in application/input-files, write something on it and store the modified version in public/images. You generate the paths of the files like this:

    // input path is '../input-files/'
    $inputPath = dirname(__FILE__).'/input-files/';            // this is an absolute path
    $inputName = 'original.jpg';
    
    // output path is '../../public/images/'
    $outputPath = dirname(dirname(__FILE__)).'/public/images/';
    $outputName = 'modified.jpg';
    
    // load image from file
    $img = imagecreatefromjpeg($inputPath.$inputName);
    
    // ...
    // write on $img here...
    // ...
    
    // save it to other file
    imagejpeg($img, $outputPath.$outputName, 95);
    

    It's not a good idea to store all the files in a single directory. Also, do not mix code with HTML/JS/CSS and never, ever, put in the same directory source files with generated files or files retrieved from external sources (uploaded by the users, for example).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥15 看一下OPENMV原理图有没有错误
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置
  • ¥15 Java计划序号重编制功能,此功能会对所有序号重新排序,排序后不改变前后置关系。
  • ¥15 关于哈夫曼树应用得到一些问题