2013-02-12 13 views
6

मैं लागू करने के लिए छवि Zend फ्रेमवर्क 2.छवि zf2

मैं उसी के लिए किसी भी घटक/सहायक नहीं पा सके में कार्यक्षमता (GD2 पुस्तकालय विस्तार के साथ अधिमानतः) का आकार बदलने की जरूरत का आकार बदलें। कोई संदर्भ?

यदि मैं एक बनाना चाहता हूं, तो मुझे इसे कहां जोड़ना चाहिए। पुराने ज़ेंड फ्रेमवर्क में, एक्शन हेल्पर की एक अवधारणा थी, ज़ेंड फ्रेमवर्क 2 के बारे में क्या?

कृपया यहां सबसे अच्छा समाधान सुझाएं।

उत्तर

17

मैं वर्तमान में Imagine का उपयोग Zend Framework 2 के साथ इसे संभालने के लिए करता हूं।

  1. कल्पना कीजिए इंस्टॉल करें: php composer.phar require imagine/Imagine:0.3.*
  2. Imagine सेवा के लिए एक सेवा कारखाना बनाएँ (YourModule::getServiceConfig में):

    return array(
        'invokables' => array(
         // defining it as invokable here, any factory will do too 
         'my_image_service' => 'Imagine\Gd\Imagine', 
        ), 
    ); 
    
  3. अपने तर्क (इसके द्वारा एक नियंत्रक के साथ एक छोटा सा उदाहरण) में यह प्रयोग करें:

    public function imageAction() 
    { 
        $file = $this->params('file'); // @todo: apply STRICT validation! 
        $width = $this->params('width', 30); // @todo: apply validation! 
        $height = $this->params('height', 30); // @todo: apply validation! 
        $imagine = $this->getServiceLocator()->get('my_image_service'); 
        $image = $imagine->open($file); 
    
        $transformation = new \Imagine\Filter\Transformation(); 
    
        $transformation->thumbnail(new \Imagine\Image\Box($width, $height)); 
        $transformation->apply($image); 
    
        $response = $this->getResponse(); 
        $response->setContent($image->get('png')); 
        $response 
         ->getHeaders() 
         ->addHeaderLine('Content-Transfer-Encoding', 'binary') 
         ->addHeaderLine('Content-Type', 'image/png') 
         ->addHeaderLine('Content-Length', mb_strlen($imageContent)); 
    
        return $response; 
    } 
    

यह स्पष्ट रूप से "त्वरित और गंदी" तरीका है, जब से तुम (फिर से प्रयोज्य के लिए वैकल्पिक लेकिन अच्छा अभ्यास) निम्न करना चाहिए:

  1. शायद एक सेवा में छवि परिवर्तनों को संभालने
  2. एक सेवा से छवियों को पुनः प्राप्त
  3. फ़ाइलें सत्यापित करने को इनपुट फ़िल्टर का उपयोग करें और
  4. कैश उत्पादन मानकों (http://zend-framework-community.634137.n4.nabble.com/How-to-handle-404-with-action-controller-td4659101.html देखने के अंत में)

संबंधित: Zend Framework - Returning Image/File using Controller

+0

क्या इसके अंदर सेवा लोकेटर का उपयोग करने के बजाय नियंत्रक को छवि सेवा (परीक्षण के लिए नकली आसान इंजेक्शन) इंजेक्ट करना बेहतर नहीं होगा? वास्तव में –

+0

। यह निश्चित रूप से इसका त्वरित और गंदा संस्करण था। मैं मजबूत आईओसी का एक मजबूत टिकाऊ हूं (http://ocramius.github.com/blog/zf2-and-symfony-service-proxies-with-doctrine-proxies/) – Ocramius

+0

@ ओक्रैमियस बहुत उपयोगी उत्तर देखें। धन्यवाद, इसे कार्यान्वित करने की जांच करेगा। – Prashant

2

इसके लिए एक सेवा का उपयोग करें और कार्यक्षमता की आवश्यकता वाले नियंत्रकों को इंजेक्ट करें।

0

तत्क्षण आप इस करना चाहिए पर अपलोड की गई छवि का आकार करने के लिए:

public function imageAction() 
{ 
// ... 
$imagine = $this->getImagineService(); 
$size = new \Imagine\Image\Box(150, 150); 
$mode = \Imagine\Image\ImageInterface::THUMBNAIL_INSET; 

$image = $imagine->open($destinationPath); 
$image->thumbnail($size, $mode)->save($destinationPath); 
// ... 
} 

public function getImagineService() 
{ 
    if ($this->imagineService === null) 
    { 
     $this->imagineService = $this->getServiceLocator()->get('my_image_service'); 
    } 
    return $this->imagineService; 
} 
2

यहाँ एक मॉड्यूल Zend फ्रेमवर्क 2. चेकआउट इस में WebinoImageThumb कहा जाता है। यह जैसे कुछ महान सुविधा है -

  • छवि का आकार बदलें
  • छवि फसल, पैड, घुमाने के लिए, दिखाने के और बचाने के छवियों
  • छवि प्रतिबिंब
2

बनाएं जो लोग Imagine एकीकृत करने में असमर्थ हैं के लिए ठीक तरह से मुझे ..

मुझे एक और समाधान मिला WebinoImageThumb here जो मेरे साथ पूरी तरह से ठीक काम करता था।

रन:: config/application.config.php में php composer.phar require webino/webino-image-thumb:dev-develop और WebinoImageThumb जोड़ने के रूप में सक्रिय मॉड्यूल जो की तरह आगे दिखता है:

<?php 
return array(
    // This should be an array of module namespaces used in the application. 
    'modules' => array(
     'Application', 
     'WebinoImageThumb' 
    ), 

.. नीचे एक ही

बनी हुई है यहाँ छोटे से स्पष्टीकरण अगर आप पूर्ण प्रलेखन पढ़ने के लिए नहीं करना चाहती है

अब इस लोकेटर सेवा के माध्यम से अपने नियंत्रक कार्रवाई प्रयोग में नीचे की तरह:

// at top on your controller 
use Zend\Validator\File\Size; 
use Zend\Validator\File\ImageSize; 
use Zend\Validator\File\IsImage; 
use Zend\Http\Request 

    // in action 
$file = $request->getFiles(); 
$fileAdapter = new \Zend\File\Transfer\Adapter\Http(); 
$imageValidator = new IsImage(); 
if ($imageValidator->isValid($file['file_url']['tmp_name'])) { 
    $fileParts = explode('.', $file['file_url']['name']); 
    $filter = new \Zend\Filter\File\Rename(array(
       "target" => "file/path/to/image." . $fileParts[1], 
       "randomize" => true, 
      )); 

    try { 
     $filePath = $filter->filter($file['file_url'])['tmp_name']; 
     $thumbnailer = $this->getServiceLocator() 
         ->get('WebinoImageThumb'); 
     $thumb = $thumbnailer->create($filePath, $options = [], $plugins = []); 
     $thumb->adaptiveResize(540, 340)->save($filePath); 

     } catch (\Exception $e) { 
      return new ViewModel(array('form' => $form, 
        'file_errors' => array($e->getMessage()))); 
     } 
    } else { 
     return new ViewModel(array('form' => $form, 
       'file_errors' => $imageValidator->getMessages())); 
    } 

शुभकामनाएं .. !!

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