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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失