@@ -119,17 +119,25 @@ def resize_image(filename):
119119 command = ["/usr/local/bin/magick" , "convert" , filepath ]
120120 if resize_value :
121121 command .extend (["-resize" , resize_value ])
122- if target_size_kb : # Ajustement de la taille cible
123- target_size = int (target_size_kb ) * 1024
124- quality = 100
125- while True :
126- temp_output = os .path .join (app .config ['OUTPUT_FOLDER' ], f"temp_{ output_filename } " )
127- command_with_quality = command + ["-quality" , str (quality ), temp_output ]
128- subprocess .run (command_with_quality , check = True )
129- if os .path .getsize (temp_output ) <= target_size or quality <= 10 :
130- output_path = temp_output
131- break
132- quality -= 5
122+
123+ # Gestion de la taille cible en Ko
124+ if target_size_kb :
125+ try :
126+ target_size = int (target_size_kb ) * 1024
127+ quality = 100
128+ while True :
129+ temp_output = os .path .join (app .config ['OUTPUT_FOLDER' ], f"temp_{ output_filename } " )
130+ command_with_quality = command + ["-quality" , str (quality ), temp_output ]
131+ subprocess .run (command_with_quality , check = True )
132+ current_size = os .path .getsize (temp_output )
133+ print (f"DEBUG: Taille actuelle = { current_size } octets, Qualité = { quality } " ) # Debug
134+ if current_size <= target_size or quality <= 10 :
135+ output_path = temp_output
136+ break
137+ quality -= 5
138+ except ValueError :
139+ flash ("Taille cible invalide." )
140+ return redirect (url_for ('resize_options' , filename = filename ))
133141 else : # Pas de taille cible
134142 if quality .isdigit () and 1 <= int (quality ) <= 100 :
135143 command .extend (["-quality" , quality ])
@@ -155,4 +163,4 @@ def download(filename):
155163 )
156164
157165if __name__ == '__main__' :
158- app .run (host = '0.0.0.0' , port = 5000 )
166+ app .run (host = '0.0.0.0' , port = 5000 )
0 commit comments