2009-12-15 9 views
9

मैं सोच रहा था कि नियंत्रक के लिए कोई तरीका था, स्ट्रिंग को वापस करने के बजाय, या एक दृश्य, एक छवि वापस करें (चाहे यह जेपीजी, पीएनजी आदि हो)। उदाहरण के लिए, $-- load-> view ('फ़ोल्डर/special_view.php) के साथ समाप्त होने के बजाय, मैं $-- load-> image (' images/gorilla.png ') जैसे कुछ करना चाहता हूं। , और ऐसा है यदि मेरा उपयोगकर्ता उस नियंत्रक पर जाना था तो वे एक छवि देखेंगे जैसे कि वे सामान्य .png या jpeg पर जाते थे। क्या मैं हेडर सेट कर सकता हूं ताकि यह एक अलग MIME की अपेक्षा कर सके? इसका उदाहरण कोड शानदार होगा।क्या कोडिनेटर नियंत्रक को एक छवि वापस करने का कोई तरीका है?

यह हमेशा मेरे लिए यह समझाने के लिए ले जाएगा कि मुझे इसकी आवश्यकता क्यों है, लेकिन इसमें एक प्रीमियर सीएमएस को कोडिनेटर में लाया जाना चाहिए, और इसे प्रमाणित चीजों को सच होने की आवश्यकता है। आपको बहुत - बहुत धन्यवाद!

उत्तर

20

वाकई, इस के बजाय $this->load->view()

$filename="/path/to/file.jpg"; //<-- specify the image file 
if(file_exists($filename)){ 
    $mime = mime_content_type($filename); //<-- detect file type 
    header('Content-Length: '.filesize($filename)); //<-- sends filesize header 
    header("Content-Type: $mime"); //<-- send mime-type header 
    header('Content-Disposition: inline; filename="'.$filename.'";'); //<-- sends filename header 
    readfile($filename); //<--reads and outputs the file onto the output buffer 
    die(); //<--cleanup 
    exit; //and exit 
} 
+0

सही काम करता है, धन्यवाद! – Ethan

+0

मैं कोडनिर्देशक के वर्तमान संस्करण पर काम कर रहा हूं। यह कोड मेरे लिए काम नहीं कर रहा है –

+0

मुझे लगता है कि 'निकास' कमांड का कोई उद्देश्य नहीं है, क्योंकि 'मर' स्क्रिप्ट को समाप्त करता है और वे वही काम करते हैं। रास्ते से कोड के लिए धन्यवाद। –

13

यह एक श्रेष्ठ बनने के रूप में इरादा नहीं है, लेकिन pǝlɐɥʞ के सुझाव एक शुद्ध पीएचपी कार्यान्वयन कि वह सब फिर से प्रयोग करने योग्य नहीं है का उपयोग कर सकते हैं। आप सिंटैक्स $ this-> load-> image ('images/gorilla.png') का उपयोग करना चाहते थे, तो यहां आप यह कैसे कर सकते हैं।

/application/libraries/MY_Loader.php बनाएं

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

/** 
* Loader Class 
* 
* Loads views and files 
* 
* @package  CodeIgniter 
* @subpackage Libraries 
* @author  Phil Sturgeon 
* @category Loader 
* @link  http://codeigniter.com/user_guide/libraries/loader.html 
*/ 
class MY_Loader extends CI_Loader { 

    function image($file_path, $mime_type_or_return = 'image/png') 
    { 
     $this->helper('file'); 

     $image_content = read_file($file_path); 

     // Image was not found 
     if($image_content === FALSE) 
     { 
      show_error('Image "'.$file_path.'" could not be found.'); 
      return FALSE; 
     } 

     // Return the image or output it? 
     if($mime_type_or_return === TRUE) 
     { 
      return $image_content; 
     } 

     header('Content-Length: '.strlen($image_content)); // sends filesize header 
     header('Content-Type: '.$mime_type_or_return); // send mime-type header 
     header('Content-Disposition: inline; filename="'.basename($file_path).'";'); // sends filename header 
     exit($image_content); // reads and outputs the file onto the output buffer 
    } 

कुछ तरीके हैं जिन्हें आप इस का उपयोग कर सकते हैं:

बेसिक उत्पादन (डिफ़ॉल्ट jpeg है)

$this->load->image('/path/to/images/gorilla.png'); 

अन्य छवि प्रकारों का उपयोग करने के लिए माइम-प्रकार भेजें

$this->load->image('/path/to/images/gorilla.jpg', 'image/jpeg'); 

वापसी छवि

$image = $this->load->image('/path/to/images/gorilla.php', TRUE); 
वैसे ही जैसे $ this-> load-> दृश्य

, 3 पैरामीटर सही पर सेट किया जा रहा मतलब है कि यह सीधे outputting के बजाय वापस आ जाएगी।

आशा इस :-)

+0

वाह! धन्यवाद! – Ethan

+0

लाइब्रेरी को एप्लिकेशन/कोर/MY_Loader.php –

+0

@GauravGupta हां में होना चाहिए, यदि आप 2.0 या बाद में उपयोग कर रहे हैं तो इसे 'कोर /' 'पुस्तकालयों /' में नहीं होना चाहिए। 2.0 200 9 में अस्तित्व में नहीं था। :) –

2

फिल के कोड के बारे में मदद करता है:

CodeIgniter 2.0 में, आज, वहाँ एक एक परिवर्तन किया है इसी क्रम में बनाने के लिए यह काम किए जाने के लिए कर रहे हैं:

  • पुस्तकालय में हो गया है/आवेदन/कोर /MY_Loader.php

मैं पुस्तकालय के विवरण के बारे में एक छोटी सी टाइपो टिप्पणी करना चाहते:

  • "(डिफ़ॉल्ट jpeg है) बेसिक उत्पादन" शीर्षक में एक गलती नहीं है क्योंकि वास्तव में डिफ़ॉल्ट है।png

समस्या के लिए एक और समाधान कर रहे हैं: यह कोर CodeIgniter पुस्तकालयों के साथ काम करने के लिए

मैं एक छोटे से कोड दिया है:

$this->output->set_header("Content-Type: image/png"); 
    $this->load->file('../images/example.png'); 

या छवि हेरफेर लाइब्रेरी का उपयोग

$config['image_library'] = "GD2"; 
    $config['source_image'] = "../images/example.png"; 
    $config['maintain_ratio'] = TRUE; 
    $config['dynamic_output'] = TRUE; 

    $this->load->library('image_lib', $config); 
    $image = $this->image_lib->resize(); 

दोनों मामलों में आपको वही छवि मिलती है जो आप प्राप्त करते हैं स्रोत लेकिन आउटपुट में।

लेकिन मेरे लिए, मैं कोर पुस्तकालय के लिए और अधिक विस्तार पसंद आया :-)

आपको बहुत बहुत धन्यवाद फिल।

+0

मैंने जेनेरिक फ़ाइल लोडर को कभी नहीं देखा। मैं कोडइग्निटर के कोड के माध्यम से जा रहा था ... इसे इंगित करने के लिए धन्यवाद .. कोडिनेटर का सर्वोत्तम उपयोग ... :) –

+0

अच्छी तरह से एक और चीज जिसे मैंने नोट किया था आंतरिक रूप से लोड फ़ंक्शन में फ़ंक्शन शामिल है ... के अनुसार php मैन्युअल में उपयोग करना चाहिए यदि इसका उपयोग प्रसंस्करण के लिए किया जाता है ... इसके बजाय readfile फ़ंक्शन का उपयोग किया जाना चाहिए ... ताकि हम फ़ाइल सहायक में उपलब्ध रीडफ़ाइल फ़ंक्शन का उपयोग कर सकें! –

0

इस विधि से काम करता है, भले ही आप $ config [ 'compress_output'] सही

$filename="/path/to/file.jpg"; //<-- specify the image file 
if(file_exists($filename)){ 
    header('Content-Length: '.filesize($filename])); //<-- sends filesize header 
    header('Content-Type: image/jpg'); //<-- send mime-type header 
    header('Content-Disposition: inline; filename="'.$filename.'";'); //<-- sends filename  header 
    $jpg = file_get_contents($filename); 
    $this->output->set_output($jpg); 
} 
1

यदि यह आपके उपयोग के मामले फिट बैठता है करने के लिए सेट है, बस इसे के लिए पुनः निर्देशित बस ठीक है। उदाहरण के लिए, छवियों का उपयोग करके ट्रैकिंग इस प्रकार होगी:

// Do your logic here 

redirect($image_path); // Or PHP's header location function 

हेडर बदलने की कोई आवश्यकता नहीं है। आपका उपयोग केस यह फिट नहीं हो सकता है, लेकिन किसी को यह उपयोगी^_^

11

स्वचालित माइम-प्रकार के साथ एक आसान तरीका मिल सकता है।

$this->load->helper('file'); 

$image_path = '/path/to/image/file'; 

$this->output->set_content_type(get_mime_by_extension($image_path)); 
$this->output->set_output(file_get_contents($image_path)); 
1

मैं एक

$computedImage = 'path/to/the/image.ext'; 
$this->load->helper('file'); 
$this->output->set_content_type(get_mime_by_extension($computedImage))->set_output(file_get_contents($computedImage)); 

करना होगा इस में मदद करता है है। चीयर्स!

संबंधित मुद्दे

 संबंधित मुद्दे