-1

I have used the wordpress add_image_size function on my theme function.php and works fine but not for what I need.

I use the function in the soft proportional crop mode ex: add_image_size('mySIZE', 420, 9999) witch means that the height of the image will change and the file name will be like "image_name-420x230.jpg".

Because I'm getting the image via AJAX I need the file name, the width will be 420 but I don't know the height of each image that I upload.

what I need is a function that resize and give the file a name without the height ex "image_name-420.jpg"

any idea on how to do this?

Thanks!

Anoop Asok
  • 1,311
  • 1
  • 8
  • 20
diegoddox
  • 593
  • 7
  • 23
  • Why do you need the file name? Create a wp_ajax function that returns the filename of the image based on the name that you gave that size. – Chris Herbert Jan 05 '15 at 07:26

1 Answers1

0

Well I find my way out.

My AJAX get a JSON what I have done is when I make my mysql query I loop through the array get the post_id.

as I have the post_id I can use the wp function get_the_post_thumbnail($posr_id, 'mySIZE') after I have the html img with the size I want I get the src from the string and push in to results array and return the JSON.

for ($i = 0; $i < count($results); $i++) {
    $img = get_the_post_thumbnail($results[$i]['id'], 'mySIZE');
    preg_match( '@src="([^"]+)"@' , $img, $match );
    $src = array_pop($match);
    array_push($results[$i], $src);
}

return json_encode($results, JSON_PRETTY_PRINT);
diegoddox
  • 593
  • 7
  • 23