o
    ʷ6i=                     @   s~   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ edZG d	d
 d
eZdS )    N)OrderedDict)NamedTemporaryFile)	smart_str)
EXTENSIONS)settings)
EngineBasez^(?:.+) (?P<x>\d+)x(?P<y>\d+)c                   @   sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )EnginezC
    Image object is a dict with source path, options and size
    c                 C   sX  t jd}||d  |d D ]}|d | }|d|  |dur+|d|  qdt|d   }g }|d d	krH|d
t jrH|d |d r[|d d	kr[|d|d   t|ddA}	|d ||	jdd	|   t
t|}tj|tjtjd}
|
  |
 \}}|rt|||	  W d   dS 1 sw   Y  dS )z,
        Writes the thumbnail image
         sourceoptionsz--%sNz%sz.%sformatJPEGprogressive	interlacequalityzQ=%drb)suffixmodez-oz[%s],stdoutstderr)r   THUMBNAIL_VIPSTHUMBNAILsplitappendr   getTHUMBNAIL_PROGRESSIVEr   namejoinmapr   
subprocessPopenPIPEwaitcommunicate	Exceptionwriteread)selfimager   	thumbnailargskvr   write_optionsfppouterr r3   p/var/www/hoanhtaovolam_webdjango/env/lib/python3.10/site-packages/sorl/thumbnail/engines/vipsthumbnail_engine.pyr&      s8   


"zEngine.writec                 C   s   t |d  d S )Nr
   )osremover(   r)   r3   r3   r4   cleanup>   s   zEngine.cleanupc                 C   sJ   t ddd}||  W d   n1 sw   Y  |jt ddS )zM
        Returns the backend image objects from a ImageFile instance
        wbF)r   deleteN)r
   r   size)r   r&   r'   r   r   )r(   r
   r/   r3   r3   r4   	get_imageA   s   zEngine.get_imagec                 C   s~   |d du r;t jd}||d  tj|tjtjd}|  t	t
|j }t|dt|df|d< |d S )z?
        Returns the image width and height as a tuple
        r;   Nr	   r
   r   xy)r   THUMBNAIL_VIPSHEADERr   r   r    r!   r"   r#   size_rematchstrr   r'   intgroup)r(   r)   r+   r0   mr3   r3   r4   get_image_sizeI   s    zEngine.get_image_sizec                 C   s~   t dd.}|| |  tjd}||j tj	|tj
tj
d}| }W d   |dkS 1 s6w   Y  |dkS )z
        vipsheader will try a lot of formats, including all those supported by
        imagemagick if compiled with magick support, this can take a while
        r9   )r   r	   r   Nr   )r   r&   flushr   r?   r   r   r   r    r!   r"   r#   )r(   raw_datar/   r+   r0   retcoder3   r3   r4   is_valid_imageV   s   


zEngine.is_valid_imagec                 C   s   d |d d< |S )Nr   rotater3   r7   r3   r3   r4   _orientationd   s   zEngine._orientationc                 C   s   |S )zc
        vipsthumbnail does not support greyscaling of images, but pretend it
        does
        r3   )r(   r)   
colorspacer3   r3   r4   _colorspacek   s   zEngine._colorspacec                 C   s$   d||f |d d< ||f|d< |S )z0
        Does the resizing of the image
        z%sx%sr   r;   r3   )r(   r)   widthheightr3   r3   r4   _scaler   s   zEngine._scaleN)__name__
__module____qualname____doc__r&   r8   r<   rF   rJ   rL   rN   rQ   r3   r3   r3   r4   r      s    )r   )r5   rer    collectionsr   django.core.files.tempr   django.utils.encodingr   sorl.thumbnail.baser   sorl.thumbnail.confr   sorl.thumbnail.engines.baser   compiler@   r   r3   r3   r3   r4   <module>   s    
