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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?